MySQL SELECT statement where something exists in List

Just recently I am back in development from support and development. So, I need to refresh my memory. Many times I have used implode() for checking whether something is in list or not using - Select ... where in list. This time list items was string and not numbers. So, I need to use different syntax this time as the old code was not correct this time.

Using implode with PHP array for numerical list:

$sql = "SELECT * FROM foo WHERE name IN (". implode(',' , $array1) .")";

The above will work when the values in $array1 are numbers and not strings, because the output will be something like this:

$sql = "SELECT * FROM foo WHERE name IN (1, 5, 6, 7)";

The values inside IN() need to be quoted for string values in PHP Array. So, for doing the same with string values in PHP array,
the query needs to be written like this:
Using implode with PHP array for string values list:

$sql = "SELECT * FROM foo WHERE name IN (". implode(" ',' ", $array1) .")";

Hope this query is correct in minor syntax. I have just posted it from the memory I have from the recent development job.

Comments are open for an year period. Please, write here on Facebook page.