Tags
Asked 3 years ago
17 Jun 2021
Views 127
Demario

Demario posted

How to include an array in WHERE clause in MySQL/PHP ?

How to include an array in WHERE clause in MySQL/PHP ?
eclipse-learner

eclipse-learner
answered Apr 28 '23 00:00

To include an array in a WHERE clause in MySQL /PHP, you can use the IN operator. Here's an example:



$my_array = array('value1', 'value2', 'value3');
$my_array_string = implode("','", $my_array);


$query = "SELECT * FROM my_table WHERE column1 IN ('$my_array_string')";
In this example, $my_array is the array of values you want to use in the WHERE clause, and $my_array_string is a string representation of the array with each value separated by a comma and enclosed in quotes. The implode() function is used to create this string.

The resulting SQL query will look like this:



SELECT * FROM my_table WHERE column1 IN ('value1', 'value2', 'value3')

This query will select all rows from my_table where the value in column1 is equal to any of the values in the $my_array array.
Post Answer