dreamfly0514 2015-04-08 07:22
浏览 32
已采纳

试图在PDO中获取非对象的属性

First of all I know that it might be a duplicate question but I did some search for example this question but I couldn't understand how it works.

I have this code...

<?php
$host       = "localhost";
$user       = "root";
$pass       = "Passw0rd";
$database   = "test";

$db = new PDO("mysql:host={$host};dbname={$database}", $user, $pass);
$stmt = $db->prepare("SELECT * FROM patient WHERE fname LIKE :q OR lname LIKE :q");
$stmt->bindValue(':q', '%'.$_GET['q'].'%');
$stmt->execute();

while ( $row = $stmt->fetch() ) {
    echo '<a href="members2.php?view=' . $row->id . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';
}
?>

And I get this error

Trying to get property of non-object in E:\xampp\htdocs\ptixiaki\livesearch.php on line 13

Line 13 is this line ...

echo '<a href="members2.php?view=' . $row->id . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';

Can you help me to fix that

  • 写回答

1条回答 默认 最新

  • doufen3563 2015-04-08 07:33
    关注

    You have to use fetchObject() instead of a regular fetch() to specify that you want to treat the row as an object (and use its properties), and not as an array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部