$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;
}
尝试在...中获取非对象的属性
我收到此错误。谁能告诉我代码有什么问题?
答案 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;
}