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"];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题