Jump to content

Need Help JOINING two tables


jeger003

Recommended Posts

hello everyone,

i am having some issues calling fields from two different tables. basically i have a classified site and one table has the title of the listing and the other has the url of the image

 

here is what i have....i wasnt able to use the actual JOIN because i have to call values from both tables.........one holds the title the other holds the url of images....and i need to fetch both at once


// JOIN (listings_urls) ON (listings_urls.listing_id = listings.id
$query = mysql_query("SELECT * FROM listings WHERE search_text LIKE '%$value_to_find%' AND live = 1 ") or die(mysql_error());

$ad_id = mysql_fetch_array($query);

$query_images = mysql_query("SELECT * FROM listings_urls WHERE listing_id = '".$ad_id['id']."' ") or die(mysql_error());

$num = mysql_num_rows($query);   

if($num > 0) 
   {
    while ($fetch = mysql_fetch_array($query) && $images = mysql_fetch_array($query_images))
      {

	 echo $fetch['title']."<br>";
         echo $images['image_url']."<br>";        	


      }
   }

else
   {
      echo 'No Items Found!';
   }

 

 

i would appreciate any help.....as you can see im new to it.....i would also appreciate pointers....and tips for future reference

Link to comment
Share on other sites

well the two tables i need to call values from are...........listings and listings_url

 

the listings table holds

id <-----------------------------

title                                        |

description                              |

location                                  |

                                              | Same numbers...these i have to JOIN

the listings_url hold                    |

id                                          |

listings_id------------------------

listings_url //is the url to the image to specific listing_id (/images/34252.jp)

 

 

 

i can use JOIN like this but........how can i call the and echo the listings_url....thats the only way to display the image specific to each listing

 

<?php


$query = mysql_query("SELECT * FROM listings WHERE search_text LIKE '%$value_to_find%' AND live = 1 JOIN (listings_urls) ON (listings_urls.listing_id = listings.id ") or die(mysql_error());

$num = mysql_num_rows($query);   

if($num > 0) 
   {
    while ($fetch = mysql_fetch_array($query) && $images = mysql_fetch_array($query_images))
      {
echo $fetch['title']."<br>";
echo $fetch['image_url']."<br>";        
      }
   }

else
   {
      echo 'No Items Found!';
   }

?>

Link to comment
Share on other sites

Of course, in your SELECT you call the fields you want.  So instead of SELECT *, use

 

SELECT listings.title, listings.image_url, listings_urls.listing_urls

 

And then in your echo, you just do the same

 

echo $fetch['listings_url']."<br>";

 

You can also view the entire $fetch array to see all the results.

Link to comment
Share on other sites

whoa....didnt know that's allowed

 

 

i made the changes and get an error only with JOIN in the query......if i remove JOIN and everything after it.....it works and its able to fetch values from both tables

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN (listings_urls) ON (listings_urls.listings_id' at line 1

 

 

this is the code that cause the error

<?php

$query = mysql_query("SELECT listings.title,listings.id,listings_urls.image_url,listings_urls.lisitings_id FROM listings,listings_urls WHERE search_text LIKE '%$value_to_find%' AND live = 1 JOIN (listings_urls) ON (listings_urls.listings_id = listings.id)") or die(mysql_error());

$num = mysql_num_rows($query);   

if($num > 0) 
   {
    while ($fetch = mysql_fetch_array($query))
      {

	 echo $fetch['title']."<br>";
         echo $fetch['image_url']."<br>";        	


      }
   }

else
   {
      echo 'No Items Found!';
   }
?>

Link to comment
Share on other sites

ok i am able to join the two tables but when i use the while() to fetch arrays it only displays one listing............anyone know why?

 

 

<?php

$id = 'listings.id';
$query_image = mysql_query("SELECT listings_urls.listings_id,listings_urls.thumb_url,listings.title,listings.image FROM listings_urls,listings WHERE listings_urls.listings_id = $id AND listings.live = 1") or die(mysql_error());

$num = mysql_num_rows($query_image);   

if($num > 0) 
   {
    while ($fetch = mysql_fetch_array($query_image) )
      {


	if($fetch['image']== 1)
	{
	 echo "<img src='/".$fetch['thumb_url']."'><br>";
	 echo "<br>".$fetch['title'];
	}
	else
	{
		echo "no image to display";					
	}


      }
   }

else
   {                                              
      echo "no books found!";
   }
?>

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.