不能终止或关闭PDO连接MySQL



当脚本结束时,页面似乎无限期地继续加载事件。如何在跳出PHP循环后终止连接?我已经将$pdo和$ userch变量设置为null,并检查了其他问题。

<?php

$pdo = new PDO("mysql:host=localhost;dbname=dbname", 'user', 'pass');
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$sql = "SELECT `counter` FROM `table` WHERE `country` LIKE 'United States'";
$start = microtime(true);
$uresult = $pdo->query($sql);
if ($uresult) {
while ($row = $uresult->fetch(PDO::FETCH_ASSOC)) {
$elapsed = microtime(true) - $start; 
if ($elapsed > 3){
echo "took more than 3 seconds";
break;
}
}
}
echo "done";
$uresult = null;
$pdo=null;
?>

我移动到MySQLi:

$mysqli  = new mysqli("localhost", "", "", "");
$uresult = $mysqli->query($sql, MYSQLI_USE_RESULT);  
$start = microtime(true);
if ($uresult) {
while ($row = $uresult->fetch_assoc()) {
$num++;
echo $num;
$elapsed = microtime(true) - $start;    
if ($elapsed  > 20){ 
echo "More than 20 seconds";    
break;    
}
}
}
$mysqli->close();

最新更新