Jump to content

trouble joining 2 tables


boba fett

Recommended Posts

I am joining 2 tables for  a web page about snakes.  I am currently using this code to join my 2 tables

 

 <?php
// Make a MySQL Connection

mysql_connect("localhost:8889","root","root") or die ('Error connecting');
mysql_select_db("snakebook");
// Construct our join query
$query = "SELECT eachsnake.Avid, specieslist.Scientific_Name, specieslist.Common_Name, specieslist.Image, specieslist.Map, eachsnake.Origin, eachsnake.Location ".
"FROM eachsnake JOIN specieslist ".
    "ON eachsnake.Scientific_Name = specieslist.Scientific_Name";
     
$result = mysql_query($query) or die(mysql_error());


// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
   
   


$avid = $row['Avid'];
$common = $row['Common_Name'];
$scientific = $row['Scientific_Name'];
$image = $row['Image'];

//display data
echo "<center><table border='0' cellpadding='5' cellspacing='0'>
";

echo "<tr>
<td><strong>$avid</strong></td>
<td><strong><i>$scientific</i></strong></td>
<td><strong>$common</strong></td>
<td>$image</td>
</tr>";
    }
?>



 

I have tried left and right joins but i only get info from one of the tables not both like i would like.

Link to comment
Share on other sites

Try the following

 

$query = "SELECT eachsnake.Avid, specieslist.Scientific_Name, specieslist.Common_Name, specieslist.Image, specieslist.Map, eachsnake.Origin, eachsnake.Location ".
"FROM eachsnake, specieslist ".
    "ON eachsnake.Scientific_Name = specieslist.Scientific_Name";

Link to comment
Share on other sites

Error Message:

 

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 'ON eachsnake.Scientific_Name = specieslist.Scientific_Name' at line 1

 

Sample of Data from eachsnake:

 

AVID | Scientific_Name | Location

010-532-375 | Crotalus atrox | (USA) TX: Hudspeth County, Dell City

058-556-069 | Crotalus viridis viridis | Captive Born 12-Aug-98 NTRC

 

Sample Data from specieslist:

 

Scientific_Name | Thumbnail | Diet

Crotalus atrox | | Mammals (mice, rats, squirrels, rabbits), lizards, and birds.

Crotalus viridis viridis | | small mammals (mice, ground squirrels, rabbits), birds, and amphibians.

 

I'd like to be able to include whatever information from the tables' columns that I like (both tables have about 10-15 columns) but can't seem to get the information to post for more than one snake.

 

Link to comment
Share on other sites

It's better to use integer values for joins, since there's a chance that the varchar values for Scientific_Name are slightly different in each table. But I'd do something like this:

 

SELECT e.*, s.* FROM eachsnake e
LEFT JOIN specieslist s ON e.Scientific_Name = s.Scientific_Name

 

See if that query works, and then you can manipulate it the way you want.

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.