简体   繁体   中英

What is wrong with this PDO statement? Cannot use object of type PDOStatement as array

I want to fetch every result from table 'Themes'.

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 />';
    }
}

Getting this error:

Fatal error: Cannot use object of type PDOStatement as array in C:\\xampp\\htdocs\\driptone\\inc\\functions.inc.php on line 137

Line 137:

    echo $select['theme_name'].'<br />';

What is the problem? Thanks.

You're using $select instead of $row inside loop.

while ($row = $select->fetch(PDO::FETCH_ASSOC))    {
    echo $row['theme_name'].'<br />';
}

使用$ row ,除了$ select

echo $row['theme_name'].'<br />';

You assigned it to $row but you're calling $select .

Should be:

while ($row = $select->fetch(PDO::FETCH_ASSOC))    
{
    echo $row['theme_name'].'<br />';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM