Fatigue has gotten the best of me . . . simple join

Discussion in 'Databases' started by Louis11, Aug 7, 2008.

  1. #1
    I have been up coding for nearly 24 hours, and it seems that I can't get a simple join statement to work. I know in the morning I'm going to call myself an idiot, but figured I would go ahead and post here in hopes of having it sorted out by the time I wake up :p

    I have two tables: category_air and images. The category table (category_air) has one column, which is 'images_id' (a reference to the primary key of the images table). The images table has several columns, primarily id and name.

    I am attempting to get all of the id's in the category table and then grab all of the image names whose id's correspond to ones found in the category_air table. I've tried several variations below and it doesn't seem to be working:

    
    select category_air.image_id, images.id, images.name from category_air inner join images on category_air.image_id = images.id
    
    Code (markup):
    Here is my complete PHP code snippet:
    
    $sql = "select category_air.image_id, images.id, images.name from category_air inner join images on category_air.image_id = images.id";
    
    $sql = mysql_query($sql) or die(mysql_error());
    
    while($row = mysql_fetch_assoc($sql))
    {
    print "Name: " . $row['images.name'] . "<br />";
    }
    
    PHP:
    It appears to be attempting to print out two rows (as it just prints 'Name:' twice) but nothing is being fetched from the database.

    Anyone see anything wrong with this code?
     
    Louis11, Aug 7, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    DId you try just doing:

    print "Name: " . $row['name'] . "<br />";

    Generally you don't use the table name in the output. Otherwise the Join looks fine.
     
    jestep, Aug 7, 2008 IP
  3. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #3
    That was it :) thanks!
     
    Louis11, Aug 7, 2008 IP