Mysql
 sql >> Database >  >> RDS >> Mysql

Passando un array a mysql

$ids = array(1, 2, 3, 4);

$ids = join(', ', $ids);
$query = "SELECT * FROM business WHERE business_id IN ($ids)";
// $query => SELECT * FROM business WHERE business_id IN (1, 2, 3, 4)

I soliti avvisi di SQL injection si applicano ancora, potresti voler scorrere prima gli ID per convalidarli o eseguirne l'escape. Inoltre, se ti aspetti delle stringhe invece dei numeri, usa questo:

$ids = array('a', 'b', 'c', 'd');

$ids = join("', '", $ids);
$query = "SELECT * FROM business WHERE business_id IN ('$ids')";
// $query => SELECT * FROM business WHERE business_id IN ('a', 'b', 'c', 'd')