我有以下代码:
$sql = $pdo->query("SELECT pageid FROM webpages WHERE ISNULL(deleted) ORDER BY sortindex");
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
switch ($row["pageid"]) {
case "information":
Write(" <li class='top'><a href='index.php?m=info' id='info' class='top_link'><span class='down'>Information</span></a>");
Write(" <ul class='sub'>");
Write(" <li><a href='index.php?m=info&submode=n'>News</a></li>");
Write(" <li><a href='index.php?m=info&submode=e'>Events</a></li>");
Write(" <li><a href='index.php?m=info&submode=m'>Minutes</a></li>");
$subsql = $pdo->query("SELECT * FROM categories WHERE visible = 1 ORDER BY sortindex,category");
while ($catrow = $subsql->fetch(PDO::FETCH_ASSOC)) Write(" <li><a href='index.php?m=info&cat={$catrow["cid"]}'>{$catrow["category"]}</a></li>");
Write(" </ul>");
break;
}
}
当我运行它时,我得到&#34;致命错误:在第29行&#34; /chroot/home/web/html/test.php中的非对象上调用成员函数fetch();第29行是&#34;而(catrow ...&#34; line。
在我的开发服务器上,它可以工作。我应该检查一下如何使其在生产服务器上运行?我不知道为什么会失败。如果我将代码移出第一个SQL循环,它在生产上工作正常。这没问题:
$sql = $pdo->query("SELECT pageid FROM webpages WHERE ISNULL(deleted) ORDER BY sortindex");
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
switch ($row["pageid"]) {
case "information":
Write(" <li class='top'><a href='index.php?m=info' id='info' class='top_link'><span class='down'>Information</span></a>");
Write(" <ul class='sub'>");
Write(" <li><a href='index.php?m=info&submode=n'>News</a></li>");
Write(" <li><a href='index.php?m=info&submode=e'>Events</a></li>");
Write(" <li><a href='index.php?m=info&submode=m'>Minutes</a></li>");
Write(" </ul>");
break;
}
}
$subsql = $pdo->query("SELECT * FROM categories WHERE visible = 1 ORDER BY sortindex,category");
while ($catrow = $subsql->fetch(PDO::FETCH_ASSOC)) Write("Cat: {$catrow["category"]}<br/>");
当我运行时,第一个循环运行,然后我得到类别列表。我知道我无法通过Stack Overflow来解决我的服务器故障,但如果有人知道什么样的配置调整或其他问题会导致这种行为,我会非常感谢任何提示:)
答案 0 :(得分:0)
未完全提取的语句可能会阻止其他语句实际发生(因此$subsql
不是有效的PDO对象)。这可能有用:
$rows = $pdo->query("SELECT pageid FROM webpages WHERE ISNULL(deleted) ORDER BY sortindex")->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
switch ($row["pageid"]) {
case "information":
Write(" <li class='top'><a href='index.php?m=info' id='info' class='top_link'><span class='down'>Information</span></a>");
Write(" <ul class='sub'>");
Write(" <li><a href='index.php?m=info&submode=n'>News</a></li>");
Write(" <li><a href='index.php?m=info&submode=e'>Events</a></li>");
Write(" <li><a href='index.php?m=info&submode=m'>Minutes</a></li>");
$subsql = $pdo->query("SELECT * FROM categories WHERE visible = 1 ORDER BY sortindex,category");
while ($catrow = $subsql->fetch(PDO::FETCH_ASSOC)) Write(" <li><a href='index.php?m=info&cat={$catrow["cid"]}'>{$catrow["category"]}</a></li>");
Write(" </ul>");
break;
}
}