【发布时间】: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"}]
【问题讨论】: