繁体   English   中英

同一查询,同一数据库,不同结果

[英]Same Query, Same Database, Different Results

我对此完全感到震惊。 当使用mysqli_query()运行时,完全相同的查询返回零结果,但通过phpmyadmin获得预期的两个结果。 我已经检查了两次和三次,我正在运行相同的查询。

PHP:

$sql = "SELECT assignment_id, next_deadline FROM " . TBL_ASSIGNMENTS . " WHERE ( next_deadline >= $after_from AND next_deadline < $after_to ) OR ( next_deadline >= $before_from AND next_deadline < $before_to ) AND stage = 1";
$result = $db->query($sql);
if ( !$result )
{
    trigger_error($db->error(), E_USER_ERROR);
}

die('Count: '.$db->numRows($result).'<br /><br />'.$sql);

$ db对象是我使用了很长时间的mysqli函数的简单包装,因此问题不存在。 包装程序正在其他地方处理类似的查询。

输出:

Count: 0

SELECT assignment_id, next_deadline FROM assignments WHERE ( next_deadline >= 1400306400 AND next_deadline < 1400310000 ) OR ( next_deadline >= 1400436000 AND next_deadline < 1400439600 ) AND stage = 1

然后,我在phpmyadmin中运行相同的SQL查询(Ced + Ped),并获得预期的2结果。

注意我也循环了结果并手动计数而不使用mysqli_num_rows,结果相同。

任何想法可能是什么原因造成的? 我的想法真的很震撼,毕竟,phpmyadmin也通过PHP运行它们。

PS,以防您怀疑包装器,以下是相关功能:

function __construct(){ 
$this->link = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
mysqli_set_charset($this->link, 'utf8');
$this->config = $this->createConfig();
// tell the server to run the close function on script shutdown
register_shutdown_function(array(&$this, 'close'));
}
function query($query){
$this->lastQuery = $query;
$this->querycount++;
$timestart = microtime(true);
$result = mysqli_query($this->link, $query);
$querytime = microtime(true) - $timestart;
$this->querytotal = $this->querytotal + $querytime;
$this->queryarray[$this->querycount] = array('query' => $query, 'time' => $querytime);
return $result;
}
function numRows($result){
return mysqli_num_rows($result);
}

在此先感谢您可能提供的任何帮助。

克里斯

编辑:现在也尝试了相同的sql查询的变体,但没有成功,包括添加了括号:

SELECT assignment_id, next_deadline FROM assignments WHERE ( ( next_deadline >= 1400306400 AND next_deadline < 1400310000 ) OR ( next_deadline >= 1400436000 AND next_deadline < 1400439600 ) ) AND stage = 1 

我还尝试过从最后删除stage = 1,以找出问题所在,并单独运行每个括号,但没有运气。 例如

SELECT assignment_id, next_deadline FROM assignments WHERE next_deadline >= 1400306400 AND next_deadline < 1400310000

从PHP doc( http://www.php.net/manual/en/mysqli-result.num-rows.php ):

如果您在执行此num_rows时遇到问题,则必须先声明-> store_result()。

$mysqli = new mysqli("localhost","root", "", "tables");

$query = $mysqli->prepare("SELECT * FROM table1");
$query->execute();
$query->store_result();

在您的情况下,请考虑在以下位置定义的过程样式: http : //www.php.net/manual/en/mysqli.store-result.php

我已设法“解决”此问题,但没有诊断出该问题!

重新启动mysql服务器(服务mysql restart)不起作用,但是重新启动服务器(reboot)却起作用。

很遗憾,这对导致问题的原因没有帮助。 如果有人有什么想法,请随时指教!

感谢您的回答,

克里斯

PM; U的原因是您指向的是不同的数据库。

SQL应该始终具有相同的行为。

请确保如果您正在从从设备读取数据并在主设备上写入数据,则同步过程将正常运行。

还要检查配置文件等。

要查看在哪里执行查询,请在数据库服务器中使用mysqladmin监视活动。

希望对您有所帮助。

耶稣加油

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM