dongzi0850 2016-11-13 15:38
浏览 34
已采纳

我无法使用sql查询数据填充数组,以将其导出为php中的json对象

I am new with php, I try to call the following function in order to populate an array of previously inserted data in mysql, but I get null.

function GetBusiness($con, $email) 
 {    
    $query = "SELECT * from Business WHERE B_EMAIL ='".$email."'";    
    $res = mysqli_query($con,$query); 
    $result_arr = array();
        while($row = mysqli_fetch_array($res))
        {
             $result_arr[] = $row;
        }
    return $result_arr;
}

and then I try to construct my json object as..

$result_business = array();
$result_business = GetBusiness($con, $email);
$json['result'] = "Success";
$json['message'] = "Successfully registered the Business";          
$json["uid"] = $result_business["id"];
$json["business"]["name"] = $result_business["name"];
$json["business"]["email"] = $result_business["email"];

but for even if the data are inserted successfully the part of $result_business is null, why I get null? I my $query typed wrong?

thank you

  • 写回答

1条回答 默认 最新

  • duanfangfei5558 2016-11-13 15:54
    关注

    The problem is, your function GetBusiness returns a multi-dimensionnal array, you can use this one instead :

    function GetBusiness($con, $email)
    {
        $query = "SELECT * from Business WHERE B_EMAIL ='".$email."'";
        $res = mysqli_query($con,$query);
        return mysqli_fetch_array($res);
    }
    

    Also, you must use the MySQL columns that you selected to access the data of the rowset. Something like

    $json["uid"] = $result_business["B_ID"];
    $json["business"]["name"] = $result_business["B_NAME"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真