I'm attempting to query a database for records where the "product_id" is included in an array of products IDs.
The array is the post result of a multiple select input (<select>
) and looks like:
$clients =
Array (
[0] => 80000016-1302638679
[1] => 8000003B-1329924004
)
I would like to pass that array to the "IN" clause of an SQL statement such as:
$sql = "SELECT * FROM sales WHERE product_id IN (".$clients.")";
...but this doesn't work (Error: Message: Array to string conversion
).
Several posts suggest using this function to format the array in a way suitable for SQL:
function format_array($array){
return implode(', ', $array);
}
}
Such as ...
$sql = "SELECT * FROM sales WHERE product_id IN (".format_array($clients).")";
That results in this query:
SELECT * FROM sales WHERE product_id IN (80000016-1302638679, 8000003B-132992400)
...and this error:
Unknown column '8000003B' in 'where clause'
What am I doing wrong? Any help is greatly appreciated! I can clarify the question if needed :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…