php在获胜时执行';t与next一起工作->;行集


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

嗨,我的电脑上有我的wamp服务器PHP 5.4.12Apache 2.4.4MYSQL 5.6.2

还有我的服务器PHP 5.5.3Apache 2.4.6MYSQL 5.5.37

当我在服务器上执行此功能时,我会出现以下错误:SQLSTATE[HY000]:一般错误,但在我的本地主机中,我没有任何错误

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

但是当我在我的服务器上做这个时,我没有错误

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上测试(

解决此问题的方法是在获取行集之前使用方法PDO::columnCount((检查列数。如果它为非零,则表示您有一个有效的行集,因此您可以调用PDO::fetchAll((。

即使PDO::nextRowset((报告为true,columnCount((也会在移动到下一个行集之前报告列数。

示例:

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

问题是您正在使用do…while表单,该表单将在验证while条件之前执行do部分中的代码。这意味着在最后一次迭代中,即使nextRowset((刚刚返回false,do部分也将最后一次执行。

只需去掉do部分,过一段时间就可以把所有东西都放进去。即使没有行集,nextRowset也不会返回true。读取do…while和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);
}

您还需要在bindParam上加一个双点,就像它绑定到的param一样

$reponse->bindParam(':nocategorie',$nocate,PDO::PARAM_INT);
while($rowset = $reponse->fetchAll(PDO::FETCH_ASSOC))
{
    $public[] = $rowset;
    $reponse->nextRowset();
}

这应该行得通。有点变化。我们检查是否存在行集,然后转到下一个行集。如果是这样的话,我们会再次这样做。现在就试试。