lightbearer74 lightbearer74 - 7 months ago 22
PHP Question

fetch_object returning blank instead of avatar?

I was using the fetch_object function to retrieve rows from my table when it stopped working after I tried retrieving the avatar column.

include "db_conx.php";
$sql = ('SELECT uid,username,avatar,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);

while($var = $result->fetch_object()->avatar){
echo $var; echo "<br />";
}


Instead of returning the avatar, it returns blank instead. I thought it would at least display the directories I had for the avatars, so I'm quite perplexed. All the other columns I selected work fine though.

Answer

You need to do in below ways:-

while($var = $result->fetch_object()){  // check change
echo $var->avatar; echo "<br />"; // check change
}

Or

while($obj = $result->fetch_assoc()){ // use assoc
   echo $var['avatar']; echo "<br />";
}
Comments