Jump to content

Help with joining two tables


dk4210

Recommended Posts

Hello Guys,

 

I am trying to do a some sort of join with a while loop and I am not sure how to do it..

 

 

I have 2 tables "ads" and "ads_photo" and I would like to pull the following data out of the tables

 

ad_id and ad_title from the "ads" table

 

The ad_photos from the "ads_photo" table

 

the "ad_photo table id has to match the id from the "ads" table

 

Then i need to display a while loop.

 

Here is what I have so far

 

$result = mysql_query("SELECT * FROM ads WHERE ad_pcat=$cid2 AND approved='1'") or die("Sql error : " . mysql_error());

while($row = mysql_fetch_assoc ($result)){
    $ad_id=$row["ad_id"];
    $ad_title = $row['ad_title'];
    
	    
    echo "<img src=\"$ad_photo\">This is the ad_title". $ad_title."ID-$ad_id"."<br>";

Please advise

Link to comment
https://forums.phpfreaks.com/topic/244914-help-with-joining-two-tables/
Share on other sites

OK Thorpe it's almost all working except one part

 

It should only display the ads with the ad_pcat=$cid AND approved='1'

 

How would I add that in the current code?

 

I tried but received an error

 

$result = mysql_query("SELECT * FROM ads LEFT JOIN ads_photo USING (ad_id);") or die("Sql error : " . mysql_error());

//$result = mysql_query("SELECT * FROM ads WHERE ad_pcat=$cid2 AND approved='1'") or die("Sql error : " . mysql_error());

while($row = mysql_fetch_assoc ($result)){
    $ad_id=$row["ad_id"];
    $ad_title = $row['ad_title'];
    $ad_photo = $row['ad_photos'];
    
  echo "This is the ad photo" . $ad_photo;
    
	    
    echo "<img src=\"$ad_photo\">This is the ad_title ". $ad_title."ID-$ad_id"."<br>";

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.