尝试获取PDO中非对象的属性 [英] Trying to get property of non-object in PDO
本文介绍了尝试获取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.
我有这个代码...
<?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/>';
}
?>
我收到此错误
Trying to get property of non-object in E:\xampp\htdocs\ptixiaki\livesearch.php on line 13
第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
推荐答案
您必须使用fetchObject()
而不是常规的fetch()
来指定您要将行视为对象(并使用其属性),而不是数组.
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.
这篇关于尝试获取PDO中非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文