需要mysqli查询的结果作为这个phpscript的json数组

时间:2017-08-18 08:51:36

标签: php json

这是我的PHP脚本

$con = mysqli_connect(HOST,USER,PASS,DB);

if($_SERVER['REQUEST_METHOD']=='GET') 
{

$qry_check="SELECT * FROM `tb_user`";

$stmt = $con->prepare($qry_check);

if ($stmt->execute()){

     echo "Success";

}}

else 
     echo "Fail";

}
?>

当我在我的mysqli在线服务器上运行此查询时,我得到的结果我已经附加到下面我需要这个结果作为json数组,当我调用这个json url,hanks提前,enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个

private void signOut() {
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    // ...
                }
            });
}

或使用此代码。在此代码中添加剩余列以获得完整结果

<?php

    $con = mysqli_connect(HOST,USER,PASS,DB);

    if($_SERVER['REQUEST_METHOD']=='GET') 
    {
        $stmt = $con->prepare("SELECT * FROM tb_user");

             if ($stmt->execute()) {
                    $users = array();
                    $user=$stmt->get_result();
                     while($row = $user->fetch_assoc()){
                        $users[]=$row;
                     }

                     $stmt->close();                       

                echo json_encode($users);
            }
}

?>

答案 1 :(得分:0)

我已在评论中提到mysqli!= PDO。但是我觉得你没有得到它。

您正在初始化与mysqli的关联,然后使用PDO执行所有流程,因此您无法获得结果

<?php  

    $servername = HOST;
    $username = USER;
    $password = PASS;
    $dbname = DB;
    $con = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

    if($_SERVER['REQUEST_METHOD']=='GET') 
    {
        $stmt = $con->prepare("SELECT * FROM tb_user");

             if ($stmt->execute()) 
             {
                    $users = array();                        
                     while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                        $users[]=$row;
                     }

                     $stmt->close();                       

                echo json_encode($users);
            }
}

?>