如何仅使用$ data数组打印SQL查询的结果?

时间:2019-06-27 20:46:43

标签: php jquery ajax

我正在尝试在php页面上打印user_reg表的所有用户。我也想使用名为$ data的数组进行检索。怎么办?

$result = mysqli_query($conn, "SELECT * FROM user_reg ORDER BY id ASC");

 $data   = array(
              'statusCode' => 200,
              'message' => 'Data retrived successfully'
);
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

echo json_encode($data);

1 个答案:

答案 0 :(得分:0)

您正在编写$data数组

$result = mysqli_query($conn, "SELECT * FROM user_reg ORDER BY id ASC");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

// write the status to a sub array
// dont destroy what you have already loaded into that array
$data['status']   = array(
       'statusCode' => 200,
       'message' => 'Data inserted successfully'
);

echo json_encode($data);