从数据库获取数据作为json响应

时间:2014-03-06 06:52:19

标签: php mysql json

这里我试图从数据库中获取一些数据,并希望将其显示为json响应,以便用户可以获取每个字段。

以下是用户可以执行查询的方式

http://localhost/safari/index.php?getbranch=true

这应该从表中提供分支细节。

这是执行此操作的PHP代码

<?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 to connect to MySQL: " . mysqli_connect_error();
            return;
        }   
        $today = date("Ymd");           

        $result = mysqli_query($con,"SELECT division, branch,branchofficename,branchofficecode,status from tbl_branchoffice");
        while ($row = @mysqli_fetch_array($result))
        {
            $result1 = json_encode($row);             
        }
        echo $result1;
    }

这有什么不妥?

JSON回复:

[{"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”, “分”: “2”, “2”: “奈”, “分支”:“奈”, “3”: “奈”, “branchofficename”: “奈”, “4”: “BR01”, “branchofficecode”: “BR01”, “5”: “活跃”, “状态”: “活跃”} ,{ “0”: “5”, “SNO”: “5”, “1”: “3”, “分”: “3”, “2”: “德里”, “分支”: “德里”, “3”: “德里”, “branchofficename”: “德里”, “4”: “BR02”, “branchofficecode”: “BR02”, “5”: “notactive”, “状态”: “notactive”},{ “0”: “6”, “SNO”: “6”, “1”: “2”, “分”: “2”, “2”: “班加罗尔”, “分支”: “班加罗尔”,“3 “:” 班加罗尔 “ ”branchofficename“: ”班加罗尔“, ”4“: ”BR03“, ”branchofficecode“: ”BR03“, ”5“: ”活跃“, ”状态“: ”活跃“},{” 0 “:” 7" , “SNO”: “7”, “1”: “3”, “分”: “3”, “2”: “浦”, “分支”: “浦”, “3”: “浦”, “branchofficename”: “浦”, “4”: “BR04”, “branchofficecode”: “BR04”, “5”: “notactive”, “状态”: “notactive”}]

2 个答案:

答案 0 :(得分:0)

$result1 = array();
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 to connect to MySQL: " . mysqli_connect_error();
        return;
    }   
    $today = date("Ymd");           

    $result = mysqli_query($con,"SELECT division,  
    branch,branchofficename,branchofficecode,status from tbl_branchoffice");
    while ($row = @mysqli_fetch_array($result))
    {
        $result1[] = $row;             
    }
    echo json_encode($result1);
}

首先从结果中收集每一行到数组result1然后最后输出$ result1数组的json_encode

答案 1 :(得分:0)

更改while循环

$result1 = array();
while ($row = @mysqli_fetch_array($result))
{    
       array_push($result1 , $row);           
 }

通过这样做,您已收集$result1

中的所有结果

现在你可以编码了

echo  $result1 = json_encode( $result1);  

我更愿意使用array,忽略json_encode行代码,

foreach($result1 as $resultset){
 //resultset contains one single row of table
    foreach($resultset as $column => $columnValue){
  //assuming your table column name as 'city'
        if($column == 'city' && $columnValue == 'pune' ){
         //displaying array of table which satisfies the condition
             var_dump($resultset );

        }
    }
}