PHP致命错误:未捕获的异常' PDOException'

时间:2013-06-20 11:23:23

标签: php mysql pdo

我正在尝试使用jQueryUI选项卡和菜单小部件创建目录列表。我有以下代码来获取目录。

<?php 

     $db = new PDO('mysql:host='.$host.';dbname='.$databaseName, 
                    $user, 
                    $password,
                    array(
                        PDO::ATTR_EMULATE_PREPARES => false,
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                  )
               );

    $menu = '';
    $charTabs = '<div id="charTabs">'; //1
    $db->exec('set names utf8');
    $queryFirstLetters = $db->prepare('SELECT DISTINCT LEFT(organization, 1) as org FROM Organizations WHERE parent_id = 0 ORDER BY organization', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));

    //Add each first characters to the charTabs
    $queryFirstLetters->execute();
    $charTabs .='<ul>'; //2
    while($rowFL = $queryFirstLetters->fetch(PDO::FETCH_ASSOC)) 
    { 
        $charTabs .= '<li><a href="#' . $rowFL['org'] . '">' . $rowFL['b'] . '</a></li>';//3
    }   
    $charTabs .= '</ul>'; //4

    //Create Divs corresponding to list items for each first letter.
    $queryFirstLetters->execute();
    while($rowFL = $queryFirstLetters->fetch(PDO::FETCH_ASSOC))
    { 
        $charTabs .= '<div id="' . $rowFL['org'] . '">'; //5

        //Get the parent organizations and add their children as sub menu items
        $queryParentOrganizations = $db->prepare('SELECT * FROM Organizations WHERE parent_id = 0 AND LEFT(organization,1) = :p1 ORDER BY organization', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
        $queryParentOrganizations->bindValue(':p1', $rowFL['org'], PDO::PARAM_STR);
        //This is the line 26 which crashes
        $queryParentOrganizations->execute(); 
        //This is the line 26 which crashes
        echo '<script>$(function(){ $("#menu'.$rowFL['org'].'").menu(); });</script>';
        $menu = '<ul id="menu'.$rowFL['org'].'">';
        while($rowPO = $queryParentOrganizations->fetch(PDO::FETCH_ASSOC))
        {
            $queryChildOrganizations = $db->prepare('SELECT * FROM Organizations WHERE parent_id = :p1 ORDER BY organization', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
            $queryChildOrganizations->bindValue(':p1', $rowPO['id'], PDO::PARAM_INT);
            $queryChildOrganizations->execute();

            $menu .= '<li><a class="list" href="#">'.$rowPO['organization'].'</a>';
            if ($queryChildOrganizations->rowCount() > 0 )
            {
                $menu .= '<ul>';
                while($rowCO = $queryChildOrganizations->fetch(PDO::FETCH_ASSOC))
                {
                    $menu .= '<li><a class="list" href="#' . $rowCO['id'] . '">' . $rowCO['organization'] . '</a></li>';
                }
                $menu .= '</ul>';
            }
            else
            {
                $menu .= '</li>';
            }
        }
        $menu .= '</ul>';
        $charTabs .= $menu . '</div>';
    }
    $charTabs .= '</div>';

    echo $charTabs;
?>

忽略性能测试,它可以正常工作。但在上传到服务器后,我收到了与PDO相关的以下错误。

  

PHP致命错误:带有消息的未捕获异常'PDOException'   'SQLSTATE [HY000]:常规错误:2014无法执行查询   其他未缓冲的查询处于活动状态。考虑使用   PDOStatement对象::使用fetchall()。或者,如果您的代码只是永远   要对mysql运行,您可以通过设置启用查询缓冲   PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性。'在   /home/server/public_html/directory/index.php:26

     

堆栈追踪:

     

# 0 /home/server/public_html/directory/index.php(26):PDO-&gt; prepare('SELECT * FROM O ...')

     

# 1 /home/server/public_html/directory/index.php(26):include('/ home / server / publ ...')

     第26行/home/server/public_html/directory/index.php中

# 2 {main}

1 个答案:

答案 0 :(得分:0)

当其他未缓冲的查询处于活动状态时,无法执行查询。考虑使用PDOStatement::fetchAll()。或者,如果您的代码只是针对mysql运行,则可以通过设置PDO::MYSQL_ATTR_USE_BUFFERED_QUERY属性来启用查询缓冲。