php只能在next-> rowset下使用 [英] php do while won't work with next->rowset
问题描述
我的电脑上有我的Wamp服务器 PHP 5.4.12 阿帕奇2.4.4 MYSQL 5.6.12
Hi i have my wamp server on my computer PHP 5.4.12 Apache 2.4.4 MYSQL 5.6.12
和我的服务器 PHP 5.5.3 阿帕奇2.4.6 MYSQL 5.5.37
And my server PHP 5.5.3 Apache 2.4.6 MYSQL 5.5.37
当我在服务器上执行此功能时,出现以下错误:SQLSTATE [HY000]:常规错误,但是在我的本地主机中没有任何错误
and when i'm doing this function on my server i have this error : SQLSTATE[HY000]: General error but in my localhost i don't have any error
function getinformationpublic($nocate)
{
try
{
$public = array();
global $Cnn;
$reponse = $Cnn->prepare("CALL GetInfoPublicCible(:nocategorie)");
$reponse->bindParam('nocategorie',$nocate,PDO::PARAM_INT);
$reponse->execute();
do {
$rowset = $reponse->fetchAll(PDO::FETCH_ASSOC);
$public[] = $rowset;
} while ($reponse->nextRowset());
$reponse->closeCursor();
return $public;
}
catch (PDOException $erreur)
{
$msg[]=$erreur->getMessage();
$_SESSION["message"]["d"]=$msg;
}
}
但是当我在服务器上执行此操作时,我没有错误
but when i'm doing this one on my server i don't have error
function getinformationpublic($nocate)
{
try
{
$public = array();
global $Cnn;
$reponse = $Cnn->prepare("CALL GetInfoPublicCible(:nocategorie)");
$reponse->bindParam('nocategorie',$nocate,PDO::PARAM_INT);
$reponse->execute();
$rowset = $reponse->fetchAll(PDO::FETCH_ASSOC);
$public[] = $rowset;
$reponse->nextRowset();
$rowset = $reponse->fetchAll(PDO::FETCH_ASSOC);
$public[] = $rowset;
$reponse->nextRowset();
$rowset = $reponse->fetchAll(PDO::FETCH_ASSOC);
$public[] = $rowset;
$reponse->nextRowset();
$rowset = $reponse->fetchAll(PDO::FETCH_ASSOC);
$public[] = $rowset;
$reponse->closeCursor();
return $public;
}
catch (PDOException $erreur)
{
$msg[]=$erreur->getMessage();
$_SESSION["message"]["d"]=$msg;
}
}
推荐答案
我对PDO :: nextRowset()遇到了同样的问题,因为即使没有可用的行集,它也会返回true,因此在调用fetchAll()时会引发HY000例外. (在PHP 5.5.12 Windows,Mysql 5.5.17 linux上进行了测试)
I had same problem with PDO::nextRowset(), as it returns true even there is no more rowsets available, therefore when calling fetchAll(), it raises exception HY000. (tested on PHP 5.5.12 windows, Mysql 5.5.17 linux)
此问题的解决方法是在获取行集之前使用方法PDO :: columnCount()检查列数.如果它不为零,则您有一个有效的行集,因此可以调用PDO :: fetchAll().
A workaround for this problem is to check number of columns with method PDO::columnCount() before fetching rowset. If it is non-zero, you have a valid rowset, and thus you could call PDO::fetchAll().
即使PDO :: nextRowset()报告为true,columnCount()也会报告移至下一个行集之前的列数.
Even if PDO::nextRowset() reports true, columnCount() will report number of columns before moving to next rowset.
示例:
while ($objQuery->columnCount()) {
$tab[] = $objQuery->fetchAll(\PDO::FETCH_ASSOC);
$objQuery->nextRowset();
}
这篇关于php只能在next-> rowset下使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!