php do while won't work with next->rowset

南楼画角 提交于 2019-12-23 21:19:56

问题


Hi i have my wamp server on my computer PHP 5.4.12 Apache 2.4.4 MYSQL 5.6.12

And my server PHP 5.5.3 Apache 2.4.6 MYSQL 5.5.37

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;
    }

}

回答1:


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)

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().

Even if PDO::nextRowset() reports true, columnCount() will report number of columns before moving to next rowset.

Example:

while ($objQuery->columnCount()) {
    $tab[] = $objQuery->fetchAll(\PDO::FETCH_ASSOC);
    $objQuery->nextRowset();
}



回答2:


The problem is you are using the do...while form, which will execute the code in the do part before verifying the while condition. This means on the last iteration, even if nextRowset() just returned false, the do part will be executed one last time.

just remove the do part, and put everything in a while. It is not true that nextRowset returns true even if there is no rowset. have a read on do...while and on nextRowset()

$public[] = $reponse->fetchAll(PDO::FETCH_ASSOC);
// so that the first rowset gets into your array
while ($reponse->nextRowset()) {
    $public[] = $reponse->fetchAll(PDO::FETCH_ASSOC);
}

also you need a double dot with your bindParam, like the param it is binded to

$reponse->bindParam(':nocategorie',$nocate,PDO::PARAM_INT);



回答3:


while($rowset = $reponse->fetchAll(PDO::FETCH_ASSOC))
{
    $public[] = $rowset;
    $reponse->nextRowset();
}

This should work. A bit of change. We check if there is a rowset then goes to next one. And then if it is we do that once more. Try it now.



来源:https://stackoverflow.com/questions/23522456/php-do-while-wont-work-with-next-rowset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!