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

 

 

 

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.