PHP MySQL how to retrieve records that match several different values in a single query -



PHP MySQL how to retrieve records that match several different values in a single query -

i have array on php contains ids of records want retrieve, i'm doing loop executing query several times them all.

foreach ($receivers $id) { $sth = $this->db->prepare("select phone contact idcontact = ?"); $sth->execute(array($id)); $res= $sth->fetch(); echo $res['phone'] ; }

how can in single query, i'll not bother database hundreds queries ?

$sth = $this->db->prepare("select phone contact idcontact in (" . implode(",",$receivers) . ")");

source: http://www.w3resource.com/mysql/comparision-functions-and-operators/in-function.php

better source: http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html

php mysql sql query-optimization

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -