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.
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: