获取记录时出现PDO错误

时间:2017-02-07 07:40:13

标签: php

$db = new PDO( 'mysql:host=localhost;dbname=mydb', 'myusername', 'mypassword' );
$query = $db->query( 'SELECT a.title, a.descrption, a.icon, b.title, b.descrption, b.images
FROM explorefeatures AS a, blog AS b WHERE a.id = b.id' );
while ( $row = $query->fetchAll( PDO::FETCH_ASSOC ) )
{
    //echo "<pre>";
    //print_r($row);
    echo $row->title;
    echo  $row->icon;
    echo $row->images;
    echo $row->descrption;
}
  

尝试在...中获取非对象的属性

我收到此错误。谁能告诉我代码有什么问题?

1 个答案:

答案 0 :(得分:0)

FETCH_ASSOC表示:&#34;嘿,PDO,获取关联数组&#34;。
关联数组一个对象。你必须像数组一样访问它:

while ( $row = $query->fetchAll( PDO::FETCH_ASSOC ) )
{
    echo $row['title'];
    echo $row['icon'];
    echo $row['images'];
    echo $row['description'];
}

或者,你可以获取作为对象。你只需要告诉PDO这样做。

while ( $row = $query->fetchAll( PDO::FETCH_OBJ ) )
{
    echo $row->title;
    echo $row->icon;
    echo $row->images;
    echo $row->description;
}

另见:PDOStatement::fetch()