dreamfly0514 2015-04-08 15: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 15: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.

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

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测