2010-03-10 79 views
6

我想在我的頁面上打印一個簡單的表格,其列表爲3列,即building name,tagsarchitecture style。如果我嘗試檢索的building namesarch. styles名單沒有問題:如何避免此PDO異常:其他無緩衝查詢處於活動狀態時無法執行查詢

SELECT buildings.name, arch_styles.style_name 
FROM buildings 
INNER JOIN buildings_arch_styles 
ON buildings.id = buildings_arch_styles.building_id 
INNER JOIN arch_styles 
ON arch_styles.id = buildings_arch_styles.arch_style_id 
LIMIT 0, 10 

我的問題上retreaving第5個標籤我剛剛寫了查詢的每一個建設開始。

SELECT DISTINCT name 
FROM tags 
INNER JOIN buildings_tags 
ON buildings_tags.tag_id = tags.id 
AND buildings_tags.building_id = 123 
LIMIT 0, 5 

查詢本身完美的作品,但不是我想使用它:

<?php 

// pdo connection allready active, i'm using mysql 
$pdo_conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); 

$sql = "SELECT buildings.name, buildings.id, arch_styles.style_name 
     FROM buildings 
     INNER JOIN buildings_arch_styles 
     ON buildings.id = buildings_arch_styles.building_id 
     INNER JOIN arch_styles 
     ON arch_styles.id = buildings_arch_styles.arch_style_id 
     LIMIT 0, 10"; 

$buildings_stmt = $pdo_conn->prepare ($sql); 
$buildings_stmt->execute(); 
$buildings = $buildings_stmt->fetchAll (PDO::FETCH_ASSOC); 

$sql = "SELECT DISTINCT name 
     FROM tags 
     INNER JOIN buildings_tags 
     ON buildings_tags.tag_id = tags.id 
     AND buildings_tags.building_id = :building_id 
     LIMIT 0, 5"; 
$tags_stmt = $pdo_conn->prepare ($sql); 

$html = "<table>"; // i'll use it to print my table 

foreach ($buildings as $building) { 
    $name = $building["name"]; 
    $style = $building["style_name"]; 
    $id = $building["id"]; 

    $tags_stmt->bindParam (":building_id", $id, PDO::PARAM_INT); 
    $tags_stmt->execute(); // the problem is HERE 
    $tags = $tags_stmt->fetchAll (PDO::FETCH_ASSOC); 

    $html .= "... $name ... $style"; 

    foreach ($tags as $current_tag) { 
     $tag = $current_tag["name"]; 
     $html .= "... $tag ..."; // let's suppose this is an area of the table where I print the first 5 tags per building 
    } 

} 
$html .= "...</table>"; 
print $html; 

我不上查詢體驗過,所以我以爲這樣的事情,但它引發錯誤:

PHP 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. 

我該怎麼做才能避免這種情況?我應該改變一切,並以不同的方式搜索這種查詢嗎?

你可以真正地再次獲取第一語句循環

回答

3

你說你發佈了一個簡化版本的代碼。當你在這裏發佈時,你有沒有改變任何東西?當您同時有多個查詢「打開」時,通常會導致此錯誤。例如,您可以撥打fetch(),但在您耗盡前您不會調用它,然後嘗試檢索第二個查詢。

從上面的代碼判斷,這不應該發生,因爲您使用的是fetchAll()。通常,解決此問題的方法是致電closeCursor()[docs]。你可以嘗試在每個fetchAll之後調用它,看看它是否做了什麼。

+0

感謝您的回覆,我會看到的文檔,我只使用fetchAll(),我只簡化了查詢長度的內部連接和表涉及的數量,如果我嘗試它在phpMyAdmin的查詢工作,它不應該' t在裏面 – vitto 2010-03-11 20:20:43

+0

是的!現在它工作!感謝幫助! – vitto 2010-03-11 20:23:33

1

(注意

$buildings_stmt->fetchAll()調用):

$tags_stmt->execute(); 
$tags = $buildings_stmt->fetchAll (PDO::FETCH_ASSOC); 

你可能想要做的是獲取$tags_stmt聲明?

$tags_stmt->execute(); 
$tags = $tags_stmt->fetchAll (PDO::FETCH_ASSOC); 
+0

對不起,通常我把我的代碼縮短以避免很長的問題,這是一個semplification錯誤,這不是問題,謝謝你的通知 – vitto 2010-03-11 11:11:12

+0

這是我的問題的解決方案,謝謝! – 2013-09-20 13:36:15

相關問題