Monday, December 28, 2015

PHP Web-Services to Return JSON Array | PHP Web services | Simple JSON Array web services




<?php
//open database connection
$con = mysql_connect('localhost','root','root') or die('Cannot connect to the DB');
mysql_select_db('mydb',$con);

//make a sql query to store data
$sql="SELECT * from `mydb`.`user`";
$qur=mysql_query($sql);

//check the database response is it true or false
 if($qur){
 if (mysql_num_rows($qur)>0) {
//write data into json array
$info = array();

while ($row = mysql_fetch_assoc($qur))
{
     $arr = array();
     $arr["name"] = $row["name"];
     $arr["email"] = $row["email"];
     $info[] = $arr;
}

$json=array("status"=>1,"message"=>"done","result"=>$info);

}
else{
$json=array("status"=>0,"message"=>"No Record found!");
}
}
else{
$json=array("status"=>0,"message"=>"error");
}


OutPut:

{
    "status": 1,
    "message": "done",
    "result": [
        {
            "name": "manish",
            "email": "manish@gmail.com"
        },
        {
            "name": "Test",
            "email": "test@tes.com"
        }      
 ]
}

Thanks,
Manish

No comments:

Post a Comment