我想在我的页面上打印一个简单的表格,其列表为3列,即building name
,tags
和architecture style
。如果我尝试检索的building names
和arch. 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.
我该怎么做才能避免这种情况?我应该改变一切,并以不同的方式搜索这种查询吗?
你可以真正地再次获取第一语句循环
感谢您的回复,我会看到的文档,我只使用fetchAll(),我只简化了查询长度的内部连接和表涉及的数量,如果我尝试它在phpMyAdmin的查询工作,它不应该' t在里面 – vitto 2010-03-11 20:20:43
是的!现在它工作!感谢帮助! – vitto 2010-03-11 20:23:33