php - getting data from database as a json response -
here trying data database , want display json response user can fetch each field.
here how user can perform query
http://localhost/safari/index.php?getbranch=true
this should give branch details tables.
here php code it
<?php if(isset($_get['getbranch'])) { $getbranch = $_get['getbranch']; if($getbranch == 'true') { getbranch($getbranch); } } function getbranch($getbranch) { $con = mysqli_connect('127.0.0.1', 'root', '', 'safari'); if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); return; } $today = date("ymd"); $result = mysqli_query($con,"select division, branch,branchofficename,branchofficecode,status tbl_branchoffice"); while ($row = @mysqli_fetch_array($result)) { $result1 = json_encode($row); } echo $result1; }
what's wrong wit this?
json response:
[{"0":"3","sno":"3","1":"2","division":"2","2":"2","branch":"2","3":"saffari travels","branchofficename":"saffari travels","4":"gfgbhghfhf","branchofficecode":"gfgbhghfhf","5":"active","status":"active"},
{"0":"4","sno":"4","1":"2","division":"2","2":"chennai","branch":"chennai","3":"chennai","branchofficename":"chennai","4":"br01","branchofficecode":"br01","5":"active","status":"active"},{"0":"5","sno":"5","1":"3","division":"3","2":"delhi","branch":"delhi","3":"delhi","branchofficename":"delhi","4":"br02","branchofficecode":"br02","5":"notactive","status":"notactive"},{"0":"6","sno":"6","1":"2","division":"2","2":"bangalore","branch":"bangalore","3":"bangalore","branchofficename":"bangalore","4":"br03","branchofficecode":"br03","5":"active","status":"active"},{"0":"7","sno":"7","1":"3","division":"3","2":"pune","branch":"pune","3":"pune","branchofficename":"pune","4":"br04","branchofficecode":"br04","5":"notactive","status":"notactive"}]
change while
loop
$result1 = array(); while ($row = @mysqli_fetch_array($result)) { array_push($result1 , $row); }
by doing so, have collected result in $result1
now can encode it
echo $result1 = json_encode( $result1);
i prefer use array
, ignor json_encode
line code,
foreach($result1 $resultset){ //resultset contains 1 single row of table foreach($resultset $column => $columnvalue){ //assuming table column name 'city' if($column == 'city' && $columnvalue == 'pune' ){ //displaying array of table satisfies condition var_dump($resultset ); } } }
Comments
Post a Comment