Skip to content
Advertisement

error when searching posts from my database

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.

$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

$pages = $stmt->fetchAll();

Try

echo $page['title'] . '<br>' ;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement