试图在 PDO 中获取 属性 个非对象
Trying to get property of non-object in PDO
首先,我知道这可能是一个重复的问题,但我做了一些搜索,例如 this question,但我不明白它是如何工作的。
我有这个代码...
<?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 行是这一行...
echo '<a href="members2.php?view=' . $row->id . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';
你能帮我解决一下吗
您必须使用 fetchObject()
而不是常规 fetch()
来指定您希望将该行视为对象(并使用其属性),而不是数组。
首先,我知道这可能是一个重复的问题,但我做了一些搜索,例如 this question,但我不明白它是如何工作的。
我有这个代码...
<?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 行是这一行...
echo '<a href="members2.php?view=' . $row->id . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';
你能帮我解决一下吗
您必须使用 fetchObject()
而不是常规 fetch()
来指定您希望将该行视为对象(并使用其属性),而不是数组。