【发布时间】:2014-07-06 01:19:11
【问题描述】:
我的想法完全被这个震撼了。当使用 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
【问题讨论】:
-
您是否在两者上运行相同版本和变体的 SQL?
-
web 应用程序和 phpmyadmin 都与数据库(MySQL 5.5.37)在同一台服务器上,连接到 localhost。两者都使用 php-fpm。更令人难以置信的是,两者都在使用 mysqli
-
我试一试,但可能带有时间戳的
WHERE过滤器在 PHPMyadmin 和 mysqli 中的工作方式不同。我的意思是,PHPMyAdmin 可以以某种方式进行操作并显示正确的结果,而 mysqli 没有该功能。 -
phpmyadmin 也在使用 mysqli,根据 phpmyadmin 的首页。我也确实认为 phpmyadmin 可能以某种方式“纠正”了 sql 查询,但是使用肯定不会是错误语法的变体会产生相同的结果(请参阅我问题底部的编辑)。
-
我知道它们是同一个数据库,但我在问他们是否使用相同版本的 SQL(也就是用于解析语句并实际检索值的系统)。可能两者之一使用的是旧版本的 mysqli。
标签: php mysql mysqli phpmyadmin