Pauli Pauli - 4 months ago 5x
MySQL Question

PHP Mysqli select value for multiple values and show the same for relection

I am trying to fetch multiple values for multiple data i know how to fetch for single data but am new to fetch multiple data

say i want to select mobile number for id 1,2,3 and show the mobile number for same with"," is what i am not able to achive


  1. Select data from Master_table For id"1,2,3"

  2. Show the Mobile_number of that particular id(s) as mention below .


name id mobile_number
abcd 1 123456789
ssss 2 123456722
sssd 3 123456733
rrrr 4 123453389
iiii 5 123444789


$query="SELECT mobile_number FROM Master_table WHERE id='1,2,3'" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());


echo $row['mobile_number'];


expected result is



You need to use MySQL IN operator:

Corrected SQL:

$query = "SELECT mobile_number FROM Master_table WHERE id IN(1,2,3)";


Select mobile_number from table where id is either 1, 2 or 3

Difference between = and IN is that with = you can compare only one value.

With IN you can compare with a set of values.

So, the flow of SQL is:

SELECT mobile_number FROM Master_table WHERE (id = 1 OR id = 2 OR id = 3)

Which is similar to:

SELECT mobile_number FROM Master_table WHERE id  IN(1,2,3)

As per OP's query, following is the updated code:

$query="SELECT  mobile_number FROM Master_table WHERE id IN( 1,2,3)" ;
$data=mysqli_query($mysqli,$query)or die(mysqli_error());
$mobiles = array();
  $mobiles[] = $row['mobile_number'];
    //echo $row['mobile_number'];
$mobilesStr = implode(',', $mobiles);
echo $mobilesStr;