0

Please I want to know if its possible to search a database table with an array for example:

$array = array('a', 'b', 'c', 'd', 'e');
$query = mysqli_query($con, "SELECT * FROM table WHERE //the possible search for each of the array value// (!in_array(table_colomn, $array)");

Is there a possible way to do this or do I have to run a for each function like so:

foreach($array as $arr){
    //run table query search for each array value
}
4
  • 2
    You are looking for the IN operator in MySQL. Commented May 23, 2016 at 14:05
  • 1
    yes... so should I run the query like this: WHERE table_colomn IN '".$array."'? Commented May 23, 2016 at 14:08
  • Close, but not quite. You need to convert the array into a string and build a proper MySQL query. Commented May 23, 2016 at 14:10
  • @KANAYOAUSTINKANE Ouch, that won't work... Commented May 23, 2016 at 14:11

1 Answer 1

4

Here is the way to do it.

$array = array('a', 'b', 'c', 'd', 'e');
$query = "SELECT * FROM table WHERE `column` IN ('".implode("', '", $array)."');";
$query = mysqli_query($con, $query);
Sign up to request clarification or add additional context in comments.

3 Comments

I fixed your quotes for you :)
lol... I forgot I don't want to get results from the array values so i guess my code will look like this WHERE column !IN ('".implode("', '", $array)."' right?
@KANAYOAUSTINKANE use "SELECT * FROM table WHERE column NOT IN ('".implode("', '", $array)."');"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.