JSON数组返回null

时间:2016-01-05 19:08:54

标签: php arrays json mysqli push

我有以下php代码:

$row=$this->conn->prepare('SELECT * FROM users');  

$row->execute();//execute the query  
$json_data=array();//create the array  
foreach($row as $r)
{  
    $json_array = array();
    $json_array['id'] = $r['id'];  
        $json_array['unique_id'] = $r['unique_id'];  
        $json_array['name'] = $r['name'];  
        $json_array['email'] = $r['email'];  
        $json_array['encrypted_password'] = $r['encrypted_password'];  
        $json_array['salt'] = $r['salt'];  
        $json_array['created_at'] = $r['created_at'];  
        $json_array['updated_at'] = $r['updated_at'];  
        $json_array['phone_number'] = $r['phone_number']; 

    //here pushing the values in to an array  
        array_push($json_data,$json_array);  

}  

//built in PHP function to encode the data in to JSON format  
return json_encode($json_data);

当我运行它时,我只能得到这个:

[{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null}]

我的数据库只包含2个用户,但没有空列。

1 个答案:

答案 0 :(得分:1)

$ row是mysqli_stmt的一个实例;你不能在foreach()中直接使用它作为mysqli_result实例中的遍历(PHP> = 5.4.0)。

$statement = $this->conn->prepare('
    SELECT
        id,
        unique_id,
        name,
        email,
        encrypted_password,
        salt,
        created_at,
        updated_at,
        phone_number
    FROM
        users
');

if ( !$statement ) {
    yourErrorHandler();
}

if ( !$statemnt->execute() ) {
    yourErrorHandler();
}

$result = $statement->get_result(); // PHP 5 >= 5.3.0,
$json_data=[];
foreach($result as $row) { // PHP 5 >= 5.4.0
    $json_data[] = $row;
}
return json_encode($json_data);

替代版本:

$result = $this->conn->query('
    SELECT
        id,
        unique_id,
        name,
        email,
        encrypted_password,
        salt,
        created_at,
        updated_at,
        phone_number
    FROM
        users
');

if ( !$result ) {
    yourErrorHandler();
}

return json_encode( $result->fetch_all(MYSQLI_ASSOC) );