Jump to content

Read from two tables in one query? (SOLVED)


Wintergreen

Recommended Posts

Any ideas why it would print it three times?

[code]$sql = "SELECT posts.title, posts.post_body, posts.poster_name, posts.post_time, posts.postid, posts.comment_number, users.avatar FROM posts, users WHERE posts.post_type = '0' ORDER BY posts.postid DESC";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
    $value = strtotime($row['post_time']);
    echo "\n" . "\t";
    echo "<img src=" . $row['avatar'] . " height=39 width=39 align=left><h1>" . $row['title'] . "<br /><span> by " . $row['poster_name'] . " on " . date("F j @ H:i", $value);
if ($_SESSION['user_level'] == 2 && $row['poster_name'] == $_SESSION['screenname']) {
echo " - <a href=edit.php?id=" . $row['postid'] . ">Edit</a>";
}
    echo '</span></h1>';
    echo '<p>' . nl2br($row['post_body']) . '<br />';
    echo "<span><a href=comment.php?id=" . $row['postid'] . ">Post a comment</a> - " . $row['comment_number'] . " comments</span></p>";
}
?>[/code]
Link to comment
Share on other sites

This is more of a hack than a proper solution, but I'm not sure of the proper way to do this.

[code]SELECT DISTINCT(posts.title), posts.post_body, posts.poster_name, posts.post_time, posts.postid, posts.comment_number, users.avatar FROM posts, users WHERE posts.post_type = '0' ORDER BY posts.postid DESC[/code]
Link to comment
Share on other sites

Awesome, you got me looking in the generally correct direction, the code that was needed was :
[code]
$sql = "SELECT posts.title, posts.post_body, posts.poster_name, posts.post_time, posts.postid, posts.comment_number, users.avatar FROM posts JOIN users WHERE posts.post_type = '0' AND users.screenname = posts.poster_name ORDER BY posts.postid DESC";
[/code]

There we go, no triple posts or anything
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.