How to manage empty IN sql query? How to manage empty IN sql query? sql sql

How to manage empty IN sql query?


you should not run the query when the $in is empty. I think what yoou need is something like this

$in = implode(',',$ids);if($in) {     $query = "SELECT * FROM user where user_id IN ($in) "; } else {      //alternate scenario here }

Edit

$in = implode("','",$ids); // generate like 1','2$query = "SELECT * FROM user where user_id IN ('$in') "; //  if has  1','2 surrond it with quote make it IN('1','2') and if empty than IN('')


Best way to manage this is:

$in = implode("','",$ids); // generate like 1','2$query = "SELECT * FROM user where user_id IN ('$in') "; //  if has  1','2 surrond it with quote make it IN('1','2') and if empty than IN('')

This saves you from if/else structure and everything else


you can add 0 ahead of $ids string. After all No ids will start from 0 in any database.

Or you can do like this..

$sql = "SELECT * FROM user where ".($ids != ''? "user_id IN ($ids)" : " 1=1");