dsgfdgh14569
dsgfdgh14569
2014-05-07 15:47
浏览 69
已采纳

php do while将无法使用next-> rowset

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

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doufei2007
    doufei2007 2014-10-15 11:54
    已采纳

    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();
    }
    
    点赞 评论
  • dongshungou7699
    dongshungou7699 2014-05-07 16:51
    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.

    点赞 评论
  • duanhuan3705
    duanhuan3705 2014-05-07 17:26

    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);
    
    点赞 评论

相关推荐