Jump to content

Problems with the a double select statement


andrew_biggart

Recommended Posts

Can anyone hep please?

 

apologys for the double post but ive just realised i have posted it in the wrong section.

 

But basiclly what i am trying to is first off al posts and info from the post table, then i want to get all the users that made a post eg $rows post_user and use this info to add to a select statement which wil then be used to slect the user_info table and select all rows where username=$rows post_user and then display prfoile_pic beside the post.

 

Im using the folowing code but the image part is not working.

 

<?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

Hi

 

You can do it with a table join. However not sure what you want from the 2nd table (I have just put a field called Join_Date from the User_infoT table to show you how to do that).

 

<?php

 

include("config_blog.php");

 

// Retrieve data from database

$sql="SELECT a.Post_username, a.Post_date, a.Post_id, a.Post_subject, b.Join_Date FROM User_postT a LEFT OUTER JOIN User_infoT b ON a.username = b.username ORDER BY Post_id DESC LIMIT 30" ;

$result=mysql_query($sql);

 

// Start looping rows in mysql database.

while($rows=mysql_fetch_array($result))

{

?>

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

<?php

// close while loop

}

 

// close connection

mysql_close();

?>

 

All the best

 

Keith

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.