PHP PDO FOUND_ROWS()返回0

时间:2014-12-09 05:14:12

标签: php pdo mysqli percona mysqlnd

我们在产品环境中使用PDO连接器,最近我们面临一个新问题。不知何故,mysql FOUND_ROWS()返回0

我还尝试在mysql控制台中手动运行SQL查询,但FOUND_ROWS()在控制台中返回正确的计数。

我发现:PDO坏了,mysqli工作

测试代码:

<?php
$db = new PDO('mysql:host=dbhost;dbname=projectname;charset=utf8', 'projectname', '***');
print_r($db->query("SELECT SQL_CALC_FOUND_ROWS `id_sales_order` FROM `sales_order` WHERE RAND() > 0.8 LIMIT 0, 1")->fetchAll(PDO::FETCH_ASSOC));
print_r($db->query("SELECT FOUND_ROWS()")->fetchAll(PDO::FETCH_ASSOC));

$mysqli = new mysqli("dbhost", "projectname", "***", "projectname");
print_r($mysqli->query("SELECT SQL_CALC_FOUND_ROWS `id_sales_order` FROM `sales_order` WHERE RAND() > 0.8 LIMIT 0, 1")->fetch_assoc());
print_r($mysqli->query("SELECT FOUND_ROWS()")->fetch_assoc());

结果:

Array
(
    [0] => Array
    (
        [id_sales_order] => 13
    )

)
Array
(
    [0] => Array
    (
        [FOUND_ROWS()] => 0
    )

)
Array
(
    [id_sales_order] => 7
)
Array
(
    [FOUND_ROWS()] => 670336
)

版本:

Mysql Server version: 5.6.21-70.0-log Percona Server (GPL), Release 70.0, Revision 688

PHP 5.4.33 (cli) (built: Sep 20 2014 16:20:03) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

MysqlI Support => enabled
Client API library version => mysqlnd 5.0.10 - 20111026 - $Id: c85105d7c6f7d70d609bb4c000257868a40840ab $

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.10 - 20111026 - $Id: c85105d7c6f7d70d609bb4c000257868a40840ab $

1 个答案:

答案 0 :(得分:0)

我之前遇到过同样的问题,但我使用fetch(PDO :: FETCH_COLUMN)修复了它。

print_r($conn->query("SELECT FOUND_ROWS()")->fetch(PDO::FETCH_COLUMN));