Jump to content

Issues selecting multiple rows


ClemenzoDK

Recommended Posts

Hello, I have some issues selecting multiple rows, and yes I'm new to both MySQL and PHP. The query is as following:

 

SELECT *, CONCAT(locations.zip_code, ' ', locations.city) as location from locations, locations_associations where locations.zip_code = locations_associations.zip_code AND client_id = 1;

 

The query works just fine, but the thing is that I have multiple client_id's stored in a PHP array which I also need to know the location of. My logic tells me that the easiest way to insert multiple client_id's into the query, would be to implode the array and then insert each client_id into the query including a seperator of some sort. The alternative would be to create an loop that inserted a client_id one at a time, which seems like a long way around contra imploding. Anyway, is it possible in this case to put in multiple client_id's in the query, and how should they be seperated, I've tried with a comma like this:

 

SELECT *, CONCAT(locations.zip_code, ' ', locations.city) as location from locations, locations_associations where locations.zip_code = locations_associations.zip_code AND client_id = 1, 2, 3, 4;

 

Which gives me an error.

 

Thanks in advance.

 

Btw, I'm running MySQL 5.5.8 on my local machine.

Link to comment
https://forums.phpfreaks.com/topic/236955-issues-selecting-multiple-rows/
Share on other sites

there is a bult in function in mysql that allows you to pass in an array as a parameter:

$array = (1,2,3,4);
SELECT *, CONCAT(locations.zip_code, ' ', locations.city) as location from locations, locations_associations where locations.zip_code = locations_associations.zip_code AND client_id IN ($array);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.