How do I resolve the following error?
Fatal error: Call to a member function fetch_assoc() on a non-object
in index.php on line 27 Call Stack: 0.0000 644064 1. {main}()
index.php:0
This is the code I’m running so far:
$sql = 'SELECT * FROM pm_user WHERE name='.$_SESSION['pmname'].'';
$result = $pdo->query($sql);
$row = $result->fetch_assoc(); // line 27
echo $row['img'];
Thanks in advance
The problems are:
You are interpolating a value inside of your query.
This is bad and wrong. You should use PDO’s prepared statements feature instead.
In the process of doing this bad and wrong interpolation, you forgot to add string quotations, making your SQL query invalid.
You failed to do any error checking whatsoever on
->query(); whenquery()fails, it returnsFALSEand thus you tried to perform->fetch()onFALSE.Fix all these things.