-1

So I was wondering what the most efficient way to search a database using WHERE.

If I have var such as

$conditions = "1,3,4,9"

With each of these ints referencing a primary id of a table. So what is the best way to select these rows from mysql.

for($i = 0; $i < strlen($conditions); $i++)
{
  if($conditions[$i] == ',')
   continue;
  $conn->prepare('select * FROM table WHERE id= $i');
}

I was thinking about doing something above however, I am sure there would be more efficient ways then this.

Ps. I know the code above won't work and i wrong it was just an example I wrote up then.

Thanks for the advice.

2 Answers2

0

The function you're looking for is find_in_set:

select * from ... where find_in_set($conditions, id)
Robert
  • 147
  • 9
0

Convert the string into array:

$conditions = explode(",","1,3,4,9");
foreach($conditions as $id){
  $conn->prepare('select * FROM table WHERE id= $id');
}