Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mysql query issue

Hello all,

I'm working on my second website using PHP and MySQL after completing the PHP development course but as I expected I'm already pretty stuck with a query. Ok so, I'm making a ecommerce computer website which has multiple tables in the database, one for products, then other ones for processors, graphics cards, motherboards etc. My products table has a bunch of id columns such as 'motherboard_id', 'processor_id' which I'm trying to use when joining the tables together. The query I am writing looks like this:

SELECT products.name, processors.name, graphics.name FROM products LEFT OUTER JOIN processors ON products.processor_id = processors.id LEFT OUTER JOIN graphics ON products.graphics_id = graphics.id
WHERE products.id = 2;

This is just a test query to see if the join works, and in phpmyadmin, it works fine, it returns the name of the product, the name of its processor, and the name of its graphics card. But, if I try to run it in PHP like this:

    try {
        $results = $db->query("SELECT products.name, processors.name, graphics.name FROM products LEFT OUTER JOIN processors ON products.processor_id = processors.id LEFT OUTER JOIN graphics ON products.graphics_id = graphics.id WHERE products.id = 2;");
    } catch (Exception $e) {
        echo "Data could not be retrieved.";
        exit;
    }

    echo "<pre>";
    $row = $results->fetchAll(PDO::FETCH_ASSOC);
    var_dump($row);
    exit;

It only returns one row, like this:

array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(15) "Nvidia Graphics"
  }
}

Really need some help with this please! :)

Thanks, Adam

3 Answers

It is because you have columns with the same name, literally. Here is a suggestion, try to make your database field names unique. Instead of a column called "name" in the products table, use "products_name". Same for graphics, instead of name use "graphics_name", that way you have no conflicts. Because all of your fields share the same name, when you retrieve your array it only shows one field. Using SQL aliases will fix the problem:

SELECT products.name AS products_name, processors.name AS processors_name, graphics.name AS graphics_name FROM products LEFT OUTER JOIN processors ON products.processor_id = processors.id LEFT OUTER JOIN graphics ON products.graphics_id = graphics.id
WHERE products.id = 2;

But every column name is prefixed by a table? I don't understand how MySQL would get confused here.. Unless this is a known issue you can link to? Plus the query hasn't changed between code and phpMyAdmin..

Thanks for the reply, it works with aliases and I'll change the column names around to make them unique :)

Hmmmm... If you run the query again in phpMyAdmin - sometimes on the results page, it shows you both the query you ran as well as the result. Has php my admin 'tidied' up any of your query?

I wouldn't know I'm still fairly new to phpmyadmin I'm just trying to practice :)

MySQL is not confused, it is the database adapter for PHP that is. The array retrieves a key which is called "name" and since all of the fields are called "name", the array overwrites the other fields.

Right okay. I find it odd, I've never come across this issue before!

It appears to be quite common