2015-02-18 56 views
0

我嘗試調用兩個存儲proceedures一個,另一個使用PHP和PDO後..但是當我嘗試這樣做,它會返回一個錯誤說無法調用MySQL的存儲過程PDO

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: 
General error: 2014 Cannot execute queries while other unbuffered queries are 
active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is 
only ever going to run against mysql, you may enable query buffering by setting 
the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' 

正如你」我也可以看到使用MYSQL_ATTR_USE_BUFFERED_QUERY屬性的即時通訊,但仍然出現錯誤。

$conn = new PDO("mysql:host=$dbConfig->host;dbname=".$dbConfig->name, $dbConfig->username, $dbConfig->password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    $conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); 

    $tables = array(); 
    $table_count = $conn->prepare("SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'milepostdb' AND table_name LIKE 'table_%' "); 
    $table_count->execute(); 

    while ($data = $table_count->fetch(PDO::FETCH_ASSOC)) { 

     $table = $data["TABLE_NAME"]; 

     //$result = $conn->prepare("SELECT * FROM $table WHERE time > DATE_ADD(NOW(), INTERVAL -1 WEEK)");//this works perfectly 
     $result = $conn->prepare("CALL sp_project_time_interval('$table')"); 

     $result->execute();// place where the error triggers 

     $count = $result->rowCount(); 
     if($count>0){ 
      $exc = $conn->prepare("CALL sp_weekly_project_report('$table')"); 
      $exc->execute(); 
      while($finalRes = $exc->fetch(PDO::FETCH_ASSOC)) 
      { 
         $tables[] = $finalRes; 
      } 
     } 

    } 
+1

而不是保持開放的結果集並遍歷它,做一個子查詢,而遍歷它,做'fetchAll'把所有的結果集在一個數組,然後遍歷數組了,這樣你可以關閉結果集在做子查詢之前。 – developerwjk 2015-02-18 00:50:02

+0

我明白你在說什麼......但爲了實現它,在技術上可以請給我一個代碼 – 2015-02-18 00:53:10

+0

的小指南konws pdo – 2015-02-18 01:23:21

回答

1

您的結果集仍然從第一個SELECT語句打開。在運行更多查詢之前,您需要關閉遊標。請注意,執行fetchAll()會自動爲您關閉光標。

$table_count = $conn->prepare("SELECT ......."); 
$table_count->execute(); 
$resultset = $table_count->fetchAll(PDO::FETCH_ASSOC); 

foreach($resultset as $data) { 
    $result = $conn->prepare("CALL ......."); 
    $result->execute(); 
    $resultset2 = $table_count->fetchAll(PDO::FETCH_ASSOC); 
}