Variables are not showing up?

I’m a bit confused here: My variables are not shown to be undefined in my error log, but they are not being shown on the page. Snippet of my code:

if (!empty($result) && $result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
      $title = $row["title"];
      $user_id = $row["discord_id"];
      $badge = " ";
      $sql = "SELECT * FROM admins WHERE admin_id=$user_id";
      $result = $conn->query($sql);


      if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
          if(isset($row["admin_id"])){
            $badge = 'fas fa-shield-alt';
          }
        }
      }
      $img = $row["img"];
      $body = $row["content"] . "...";
      $post_id = $row["post_id"];
      $author = $row["author"];
      echo "<center>";
      echo "<hr>";
      echo "<a href='/post?id=${post_id}'>";
      echo "<div class='post'>";
      echo "<strong>${title}</strong> - <a href='/users?id=${user_id}'>${author}</a><i class='" . $badge ."'></i><br>";
      echo "<small>${body}</small>";
      echo "</div>";
      echo "</a>";
      echo "<hr>";
      echo "</center>";
      echo "<!-- " . $author . "-->";
    }   
}

This is the HTML that creates:

<center>
<hr>
<a href='/post?id='>
<div class='post'>
<strong>New Ideas</strong> - <a href='/users?id=466262009256869889'></a>
<i class='fas fa-shield-alt'></i>
<br>
<small>...</small>
</div>
</a>
<hr>
</center>
<!-- -->

As you can see, the author variable and post_id variables are not loading in and nothing is showing up in my error logs (this code is past line 100).

What is causing this?

Bumped

You are overriding your $row variable in the nested while loop if your sql query returns any rows. Unless that is intentional, it is the source of the problem.

if (!empty($result) && $result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    // $row is set to some value
    
    $sql = "SELECT * FROM admins WHERE admin_id=$user_id";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
      // below you override $row
      while($row = $result->fetch_assoc()) {
        if(isset($row["admin_id"])){
          $badge = 'fas fa-shield-alt';
        }
      }
    }
    
    // $row is now something else if your if-statement was true
    
    // echos
  }   
}
1 Like

Ok, that makes sense! Thank you.