PHP PDO为SELECT FOUND_ROWS()返回不一致的结果 [英] PHP PDO returning inconsistent results for SELECT FOUND_ROWS()
问题描述
我在使用PHP/PDO和MySQL时遇到问题
I have an issue with PHP/PDO and MySQL
当我使用SQL_CALC_FOUND_ROWS运行查询然后选择FOUND_ROWS()时,80%的时间返回0,其余时间FOUND_ROWS是准确的
When I run a query with SQL_CALC_FOUND_ROWS and then select FOUND_ROWS(), 80% of the time its returning 0, and the rest of the time FOUND_ROWS is accurate
我已经将其简化为一个简单的测试循环,但这在我的开发服务器上可以正常工作,但是在生产中,测试用例存在不一致问题.
I've reduced it to a simple test loop, but this works fine on my dev server, but in production the test case is having the inconsistency problem.
即使在生产环境中,从MySQL命令行运行相同的查询也可以正常工作,因此这似乎是PHP/PDO问题
Running the same queries from MySQL command line works correctly even in production, so it appears to be a PHP/PDO problem
PHP 5.5.28-使用mysqlnd 5.0.11-dev CentOS 6.6(最终版)上的Percona Server 5.6.25-73.1-日志
PHP 5.5.28 - using mysqlnd 5.0.11-dev Percona Server 5.6.25-73.1-log on CentOS 6.6 (Final)
任何人都可以帮忙吗?我已经尽力想了一切,然后把头发拔掉了
Can anyone help please? I've tried everything I can think of, and I'm tearing my hair out
<?php
require_once "../consts.php";
$nolimit_query = "select SQL_CALC_FOUND_ROWS targetusers.u_id
FROM
users targetusers
LEFT JOIN user_extra targetuserextra ON (targetuserextra.ue_userid = targetusers.u_id)
LEFT JOIN countries ON (c_id = targetusers.u_country)
LEFT JOIN cities ON (cities.ct_countryid = c_id and cities.ct_id = targetusers.u_city)
LEFT JOIN userimages ON (targetusers.u_primaryimage = userimages.ui_id and userimages.ui_userid = targetusers.u_id and userimages.ui_imagetype = 'P')
WHERE
(targetusers.u_deleted = 0) and
(targetusers.u_id NOT IN (19, 32, 115)) and
(targetusers.u_active = 1) and
(targetusers.u_confirmed=5) order by u_lastupdated DESC LIMIT 10, 10";
// already tried this soln suggested elsewhere -- defaults to off for my PHP anyway though
ini_set("mysql.trace_mode", 0);
ini_set("display_errors", 1);
// set up PDO connection
$dbh = new PDO(
"mysql:host=" . Config::Get()->DB_SERVER . ";dbname=" . Config::Get()->DATABASE_NAME . ";charset=utf8",
Config::Get()->DB_USERNAME,
Config::Get()->DB_PASSWORD
);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_PERSISTENT, false);
echo $dbh->getAttribute(PDO::ATTR_CLIENT_VERSION) . "<br>";
echo $dbh->getAttribute(PDO::ATTR_DRIVER_NAME) . "<br>";
echo $dbh->getAttribute(PDO::ATTR_SERVER_INFO) . "<br>";
echo $dbh->getAttribute(PDO::ATTR_SERVER_VERSION) . "<br><br>";
echo date("r") . "<br>";
for ($i=0; $i<5; $i++)
{
// run query above
$stmt = $dbh->prepare($nolimit_query);
if ($stmt === FALSE)
{
die($stmt->errorInfo());
}
// get result set
if (($result = $stmt->execute()) === TRUE)
{
$a = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$stmt = NULL;
// this query returns inconsistent results (80% of the time it returns 0!)
$b = $dbh->query("SELECT FOUND_ROWS() as cnt")->fetch(PDO::FETCH_ASSOC);
echo "FOUND ROWS = " . $b["cnt"] . "<br>";
}
}
此脚本的输出如下.
请注意,在5个循环中,只有2个返回了FOUND_ROWS的正确值,其他3个返回为0
Note that only 2 of the 5 loops are returning the correct value for FOUND_ROWS, with the other 3 coming back as 0
mysqlnd 5.0.11-dev - 20120503 - $Id: 15d5c781cfcad91193dceae1d2cdd127674ddb3e $
mysql
Uptime: 1857223 Threads: 1 Questions: 4446069 Slow queries: 32 Opens: 465 Flush tables: 1 Open tables: 403 Queries per second avg: 2.393
5.6.25-73.1
Fri, 04 Sep 2015 12:01:10 +0100
FOUND ROWS = 0
FOUND ROWS = 0
FOUND ROWS = 59836
FOUND ROWS = 0
FOUND ROWS = 59836
推荐答案
问题已解决.事实证明,NewRelic Application Monitoring守护程序或扩展正在干扰FOUND_ROWS()的结果
Problem solved. It turns out the NewRelic Application Monitoring Daemon or extension was interfering with the results of FOUND_ROWS()
那太糟糕了:(
禁用扩展名和FOUND_ROWS再次防弹
Disabling the extension and FOUND_ROWS is bulletproof again
这篇关于PHP PDO为SELECT FOUND_ROWS()返回不一致的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!