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


i trying fetch multiple values multiple data know how fetch single data new fetch multiple data

say want select mobile number id 1,2,3 , show mobile number same with"," not able achive

expected

  1. select data master_table id"1,2,3"
  2. show mobile_number of particular id(s) mention below .

database

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

php

$query="select  mobile_number master_table   id='1,2,3'" ; $data=mysqli_query($mysqli,$query)or die(mysqli_error());  while($row=mysqli_fetch_array($data)){      echo $row['mobile_number'];  } 

expected result

123456789,123456722,123456733 

you need use mysql in operator:

corrected sql:

$query = "select mobile_number master_table id in(1,2,3)"; 

explanation:

select mobile_number table id either 1, 2 or 3

difference between = , in = can compare 1 value.

with in can compare set of values.

so, flow of sql is:

select mobile_number master_table (id = 1 or id = 2 or id = 3) 

which similar to:

select mobile_number master_table id  in(1,2,3) 

as per op's query, following updated code:

$query="select  mobile_number master_table id in( 1,2,3)" ; $data=mysqli_query($mysqli,$query)or die(mysqli_error()); $mobiles = array(); while($row=mysqli_fetch_array($data)){   $mobiles[] = $row['mobile_number'];     //echo $row['mobile_number']; } $mobilesstr = implode(',', $mobiles); echo $mobilesstr; 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -