Mike
Mike

Reputation: 105

Getting multiple row ID's without loop

The values I have are in a session. I can't use JOIN (I assume) because that is for multiple tables. I'm working with one table.

The table is called 'VEHICLES'. Each row has a field called 'car' and has an AUTO INCREMENTED ID. I need the ID for each $car*. So I would like a query that will pull ID's only if $car* exists.

There will always be at least one $car*. But 2, 3, 4 cars etc... are possible. How can check for, and query these possibly existing $cars?

$car1 = 'Honda';
$car2 = 'Mazda';
$car3 = '';

$query="SELECT
  id( WHERE car='$car1') as My_car1,
  id( WHERE car='$car2') as My_car2,
FROM VEHICLES";

list($My_car1,$My_car2)=pdo_fetch_array(pdo_query($query));

I realize my query is wrong and will not work. But it was the only way I could visually explain what I am trying to do.

Upvotes: 0

Views: 99

Answers (1)

Marc B
Marc B

Reputation: 360572

Basic SQL:

SELECT id
FROM VEHICLES
WHERE car IN ('Honda', 'Mazda')

And as a general tip, NEVER chain DB calls the way you are. DB operations can/will fail, and you're assuming success. if any of the "inner" calls fail, they'll return a boolean false and break the outer calls. You'll be left in the dark as to why things failed.

NEVER assume success when dealing with external resources. Always assume everything will fail, CHECK for failure at every stage, and treat success as a pleasant surprise.

Upvotes: 1

Related Questions