I’m getting an error when I am searching through my database for certain posts.
it says this: Trying to get property ‘title’ of non-object on line 83
Below is the bit of code it refers to, I’m fairly new to PHP and PDO so bear with me.
JavaScript
x
$search = '%post%';
$sql = 'SELECT * FROM pages WHERE title LIKE ?';
$stmt = $conn->prepare($sql);
$stmt->execute([$search]);
$pages = $stmt->fetchAll();
foreach($pages as $page){
echo $page->title . '<br>' ;
}
Advertisement
Answer
This returns an array not an object
JavaScript
$pages = $stmt->fetchAll();
Try
JavaScript
echo $page['title'] . '<br>' ;