How do I use in_array() function in where condition in MYSQL?
It should check all key of Array
$arr=array(1,2,3,4,5);
$query="select * from tablename where id='$arr'";
How do I use in_array() function in where condition in MYSQL?
It should check all key of Array
$arr=array(1,2,3,4,5);
$query="select * from tablename where id='$arr'";
MySQL uses the syntax WHERE id IN (1, 2, 3, 4, 5)
. You can use implode()
to convert your array to that kind of string.
$arr = array(1, 2, 3, 4, 5);
$list = implode(",", $arr);
$query = "select * FROM tablename WHERE id IN ($list)";
You can make use of mysql 'IN' clause. Step 1 : implode your array by using PHP implode function. Step 2 : Pass that array to mysql query using IN clause.
Eg :
$array = array(1,2,3);
$implArray = implode(",",$array);
$query = "select * from `tablename` where `id` IN ($implArray)";
Above condition will work where array values are numeric.
For Alphanumeric Values, you will have to quote the values. eg :
$array = array('A','B','C');
$implArray = implode("','", $array);
$query = "select * from `tablename` where `id` IN ('"'.$implArray.'"')";