Aaron Robeson
Aaron Robeson

Reputation: 312

PHP echo $row['username'] not displaying the first username?

I'm not really sure what the problem is here, I have a posts table that has a foreign key of userid and I want to pull their usernames from the users table based on that, it seems to pull them out fine but it won't display on the first post. My code is:

$query_therealuserid = 'SELECT users.username, users.userid, posts.userid, posts.created_at
FROM users, posts
WHERE users.userid = posts.userid
ORDER BY posts.created_at';

$therealuserid = mysql_query($query_therealuserid, $akaearthdb) or die(mysql_error());

and

<?php do { ?>
  <div id= "post">
    <?php if (stripos($row_rsjustpost['link'], ".png") !== false) {?>
            <img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
            <?php }?>
    <?php if (stripos($row_rsjustpost['link'], ".gif") !== false) {?>
            <img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
            <?php }?>
    <?php if (stripos($row_rsjustpost['link'], ".jpg") !== false) {?>
            <img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
            <?php }?>
    <?php if (stripos($row_rsjustpost['link'], ".jpeg") !== false) {?>
            <img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
            <?php }?> 

    <a href="<?php echo $row_rsjustpost['link']; ?>"><?php echo $row_rsjustpost['title']; ?></a>
    <br/>
    <?php echo $row_rsjustpost['text']; ?>
    </p>
    <br />

            <?php 
            do {
            if ($whileLoopCounter0!=$whileLoopCounter1){break;}
        ?>By: <?php echo $row['username'];
        echo "<br />";
            $whileLoopCounter1++;

            } while($row = mysql_fetch_array($therealuserid));
            ?>


  </div>
  <?php $whileLoopCounter0++; ?>
  <?php } while ($row_rsjustpost = mysql_fetch_assoc($rsjustpost)); ?>
</div>

when I pull up the page I get all the posts but the first one doesn't have a username and the usernames are all pushed down one post. The first post has a postid of 2, if I create a new post with a postid of 1 and a userid it shows up at the top without a username and the others usernames are moved up so that they are corect.

Upvotes: 0

Views: 2975

Answers (3)

Tushar
Tushar

Reputation: 8049

You're currently using a do-while loop, so first the do block is executed and then the while block is evaluated only after the 1st execution of the do block.

Use this instead:

while ($whileLoopCounter0==$whileLoopCounter1) {
   $row = mysql_fetch_array($therealuserid);
   if ($row){
 ?>
 By: 
 <?php 
      echo $row['username'];
      echo "<br />";
      $whileLoopCounter1++;
    }
}

Upvotes: 1

M Noivad
M Noivad

Reputation: 106

Looks like you are fetching the username after you increment the loop counter. The user name is pulled after the loops is one, so the first row will be empty because that data doesn’t exist yet.

There are a lot of other things though that you’re doing that are just wasting effort though. Try tossing the results into an object and using foreach($result as $key=>$value) or foreach($result as $item) if you do not need the key instead.

Upvotes: 0

Musa
Musa

Reputation: 97672

That's because you use a do-while loop, the $row = mysql_fetch_array($therealuserid) is executed at the end of the loop, why not use a regular while loop.

Upvotes: 3

Related Questions