Pass array in mysql statement using php

I need to archive something like this:

$array['0']="ID='1'";
$array['1']="ID='2'";

mysql_query("SELECT * FROM users WHERE {$array}");

      

This is a special condition and this is just an example; I am not worried about security here as there is no value for the end user. The array values ​​are automatically generated and can be of any size, so I cannot manually pass them.

+3


source to share


1 answer


only store the values ​​in the array.

$array = ('aaa', 'bbb', 'ccc');

mysql_query("SELECT * FROM users WHERE ID IN ('".implode("','", $array)."')");

      

This will hold the array together and output something like:

mysql_query("SELECT * FROM users WHERE ID IN('aaa','bbb','ccc')");

      



you can leave '

if you only use integers. But everything related to strings needs them in an SQL statement, so it's better to use them from getgo.

If you check other things you shoudl stick with thos and AND

or OR

whichever you want to check them and maybe put them in different arrays

mysql_query("SELECT * FROM users WHERE ID IN ('".implode("','", $array_1 )."') OR name IN ('".implode("','", $array_2 )."')");

      

+2


source







All Articles