[英]What is wrong with this PDO statement? Cannot use object of type PDOStatement as array
我想從表“主題”中獲取所有結果。
function display_all_themes()
{
global $pdo;
$select = $pdo->prepare("SELECT * FROM themes");
$select->execute();
while ($row = $select->fetch(PDO::FETCH_ASSOC))
{
echo $select['theme_name'].'<br />';
}
}
收到此錯誤:
致命錯誤:無法在第137行的C:\\ xampp \\ htdocs \\ driptone \\ inc \\ functions.inc.php中將PDOStatement類型的對象用作數組
第137行:
echo $select['theme_name'].'<br />';
問題是什么? 謝謝。
您正在使用$select
而不是$row
內部循環。
while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
echo $row['theme_name'].'<br />';
}
使用$ row ,除了$ select
echo $row['theme_name'].'<br />';
您已將其分配給$row
但正在調用$select
。
應該:
while ($row = $select->fetch(PDO::FETCH_ASSOC))
{
echo $row['theme_name'].'<br />';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.