Jump to content

Problems with 2 select statements!


andrew_biggart

Recommended Posts

What im trying to do is first of all select all of the recent posts made by users and display them and then i want so get the usernames of the people who made the post, once i have got these i want to select all users which are = to $rows post_usersnames and get there profile pic and display it beside the post.

 

Its hard to explain this properly when ur this dumb lol but hopefully u can make some sense of it. Im using the following code to try and do it but im just getting a banc screen, although when i remove the scond select statement the post info dispays correctly.

 

<?php

include("config_blog.php");


// Retrieve data from database 
$sql="SELECT * FROM User_postT ORDER BY Post_id DESC LIMIT 30" ;
$result=mysql_query($sql);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){

// Retrieve data from database 
$sql2="SELECT * FROM User_infoT WHERE username=$rows Post_username" ;
$result2=mysql_query($sql2);

// Start looping rows in mysql database.
while($rows2=mysql_fetch_array($result2)){

?>
<table class="profileforum">
<tr><td style="width: 55px"><? echo "<img src='../Thumbnail_images/". $rows2['Profile_picture'] . "' style='width:50px; height:50px;' />";?></td>
<td>
<table class="forum_post" cellspacing="0" cellpadding="0" style="width: 345px; height: 52px">
<tr><td class="forum_sub">Posted by <a class="posts1" href="profile.php?username=<? echo $rows['Post_username']; ?>"><? echo $rows['Post_username']; ?></a> on <? echo $rows['Post_date']; ?></td></tr>
<tr><td class="forum_h" valign="top"><a class="posts2" href="view_topic.php?Post_id=<? echo $rows['Post_id']; ?>"><? echo $rows['Post_subject']; ?></a></td></tr>
</table>
</td></tr>	
</table>
<?
// close while loop 
}

// close connection 
mysql_close();
?>

Link to comment
Share on other sites

I'm not an expert at Joins in MySQL... I tend to do most of my database coding with the Oracle database, and have got into the bad habit of using Oracle's proprietary "shortcut" syntax for joins. You're probably better trying to get one of the mySQL experts on the forum to help with the syntax for a join.

 

However:

$sql2="SELECT * FROM User_infoT WHERE username='".$rows['Post_username']."'";

might fix your problem in the interim.

 

The JOIN really is a better solution though. If your "outer" SQL query returns the 30 row limit, then you're executing an additional 30 SQL queries in the "inner" loop, totalling 31 queries. Using a JOIN, this number would be reduced to 1 query. Database access is slow, so the JOIN would significantly improve the speed of your script, and the code logic would be simpler with only a single while loop.

 

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.