Retrieving values from MySQL using php -


here have made mysql has columns of "id","name","username","email",age" . made the php code retrieve data given id.

eg:if user enter id=3 shows datas corresponding id.

but want set multiple inputs user can type more 1 id , list corresponding datas of particular id.

my php code:

<?php   if($_server['request_method']=='get'){      $id  = $_get['id'];         require_once('dbconnect.php');      $sql = "select * user id='".$id."'";      $r = mysqli_query($con,$sql);      $result = array();              while($res = mysqli_fetch_array($r)){      array_push($result,array(         "id"=>$res['id'],         "name"=>$res['name'],         "username"=>$res['username'],         "email"=>$res['email'],          "age"=>$res['age']         )     );             }     echo json_encode(array("result"=>$result));      mysqli_close($con);  }  

now url gives perfect result: "http://www.allwaysready.16mb.com/sort.php?id=4"

now how can corresponding values multiple id's?

you can use array syntax pass multiple ids script , use mysql's in() query against them @ once.

url: http://www.allwaysready.16mb.com/sort.php?id[]=4&id[]1&id[]=2

$ids = $_get['id']; $ids = array_map(function($id) {     return (int) $id; }, $ids); $ids = implode(',', $ids);  $sql = "select * user work in($ids); 

i cast ids integers because current code wide open sql injection. should using paramterized queries.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -