lightbearer74 lightbearer74 - 1 year ago 100
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 Source

You need to do in below ways:-

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


while($obj = $result->fetch_assoc()){ // use assoc
   echo $var['avatar']; echo "<br />";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download