Home > OS >  Why is this SQL statement (SELECT * FROM items WHERE id=2) returning only the first instance?
Why is this SQL statement (SELECT * FROM items WHERE id=2) returning only the first instance?

Time:03-09

I have the following table:

items:
id |color| price
1   red   10
2   red   11
2   blue  11.5
3   green 9

id and color make up the key. i want to return all items with id=2.

I tried using this statement

SELECT * FROM items WHERE id=2

but the output is only

Array([id] => 2 [color] => red [price]=>11)

Why doesnt it return both of the items of id = 2 (the red and blue) as expected? How do I return all items with id=2??

CodePudding user response:

This should not be possible because a primary key is unique so you should only have one item with the id of 2.

CodePudding user response:

most likely you are using

fetch(); //which Fetches the next row from a result set

instead of

fetchALL(); //which Returns an array containing all of the result set rows

if this is not the case please edit your question and add your complete code

  • Related