【问题标题】:getting data from database as a json response从数据库中获取数据作为 json 响应
【发布时间】:2014-03-06 06:52:18
【问题描述】:

在这里,我试图从数据库中获取一些数据,并希望将其显示为 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","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"}]

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    更改您的 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 );
    
            }
        }
    }
    

    【讨论】:

    • 谢谢,您能告诉我以更好的可视化方式显示 json 响应吗?如何从上面的 json 响应中查询获得 city = pune 的记录?
    • 为什么不使用数组?
    • 再次如果要在json中搜索,则需要将其转换为数组,然后根据key为city和value为pune查找
    • 我不太熟悉,所以无法决定哪种方式更适合我。你能告诉我上面哪种方法是正确的吗?再次感谢
    • 那么您的代码在array_push($result1 , $row); 行给出错误Warning: array_push() expects parameter 1 to be array, null given in
    【解决方案2】:
    $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

    【讨论】:

    • 谢谢,您能告诉我以更好的可视化方式显示 json 响应吗?如何从上面的 json 响应中查询获得 city = pune 的记录?
    猜你喜欢
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2020-04-13
    • 2023-03-12
    • 2016-10-18
    • 1970-01-01
    相关资源
    最近更新 更多