0

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.

3
  • 1
    Why do you have 4 conditions which do the same? Commented May 8, 2012 at 20:06
  • 1
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented May 8, 2012 at 20:07
  • @Truth Sorry about that I'm just getting started and I'm following some somewhat old tutorials as well as using dreamweaver's tools to write some of the code. I'll look into PDO and MySQLi, thanks. Commented May 9, 2012 at 14:04

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was avoiding it because even if the break was called it would count as if it had displayed a username, so it would only display every other one. (Tushar Dhoot fixed it for me)
1

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++;
    }
}

3 Comments

I tried that but it counts as having gone through an iteration even if the if statement breaks out of the loop, so it only displays every other username.
It worked! Thank you, I've been trying to get this to work for like a week now. :)
Glad I could help! Take a look at the PDO suggestion above, it's very good advice.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.