假设我有一个具有以下布局的数据库:

Fields:  |Business_Name| |Business_ID|

Data  :  |business_1A  | |ABC_1      |

我希望查询数据库,同时检索业务名称和业务ID,然后将此结果编码为JSON以供进一步使用。
我该怎么做?
下面是一些要求的代码:
$sql = "SELECT Business_Name,Business_ID FROM biz_table";

$businessArray = array();
$bizResult = mysql_query($sql);

        while($row = mysql_fetch_assoc($bizResult)) {
            $businessArray[][] = $row['Business_Name']$row['Business_ID'];
            }
            $result = json_encode($businessArray);
            echo $result;

提前谢谢!

最佳答案

基于你的问题-你的问题是“我该怎么做?”不需要给出您想要的输出示例:

$sql = "SELECT Business_Name,Business_ID FROM biz_table";

$businessArray = array();
$bizResult = mysql_query($sql);

while($row = mysql_fetch_assoc($bizResult)) {
    $businessArray[$row['Business_Name']] = $row['Business_ID'];
}
$result = json_encode($businessArray);
echo $result;

$result将是
{"business_1A" : "ABC_1" }
这就是你想要的吗?

07-22 02:07