Help: Simple array/recalling mysql php...

Discussion in 'PHP' started by Skillman13, Dec 3, 2009.

  1. #1
    I have a custom forum i'm making...

    And I have a section of code that (should) display all the posts in that thread...

    The code works, but only for the first post... It doesn't add the second/third (etc...) posts...


    Can anyone find the problem with this code?


    $query = ("SELECT * FROM `Forum3` WHERE `ThreadId` = '$id'");
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)) {
    $username = $row['Username'];
    $post = $row['Post'];
    $query = ("SELECT * FROM `Zombie` WHERE `Username` = '$username'");
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    echo "<a href= 'username.php?" . "&id=". urlencode($row['Id']) . "'>". $row['Username'] ."</a>". ": ". $post;

    echo "<br>";
    }



    Thanks alot,

    James
     
    Skillman13, Dec 3, 2009 IP
  2. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You're overwriting the $row and $result variables with the second query. Rename them to something else and it should work.
     
    kingsoflegend, Dec 3, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I take it each post in your table has the user id, so you need just one query
    SELECT * FROM `forum_post_table`, `user_table` WHERE `forum_post_table`.`user_key` = `user_table`.`user_key` AND `forum_post_table`.`thread_unique_key` = 'thread id here'

    Something along those lines should do it. Replace the tables and columns with your ones and you should be fine
     
    JAY6390, Dec 3, 2009 IP
  4. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks works now, :)
     
    Skillman13, Dec 3, 2009 IP