php - 相同的查询,相同的数据库,不同的结果

标签 php mysql mysqli phpmyadmin

我的心被这件事彻底打动了。当使用 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 文档(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

关于php - 相同的查询,相同的数据库,不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714734/

相关文章:

MySQL 按总和限制

Java JDBC MySQL 插入 TIMESTAMP 类型失败

php - 如何在codeigniter中依次更新两个表数据?

php - 获取 URL 数据时推荐系统的重定向问题

php - INSERT/VALUES 未提交

php - CodeIgniter 和 PHP 5.5.14 - mysql_escape_string() 问题

php - 在 plesk api 中,如果我不知道数据库 ID,如何将用户添加到数据库

php - 除了sql注入(inject)还有什么?

php - 执行 mySql 的 php 循环中的错误处理

PHP 简单 HTML DOM 解析器 : Select only DIVs with multiple classes