我想填充一个多维数组并将其从php发送到js。
首先我执行sql查询
$query = "SELECT value1, value2, value3 FROM table WHERE id = '$id' LIMIT 10";
$value = mysqli_query($db, $query);
下一步是填充数组
$data = array();
$counter = 1;
while($row = mysqli_fetch_assoc( $value )){
$data[$counter] = array($row['value1'], $row['value2'], $row['value3']);
$counter++;
}
但是当我尝试使用
测试输出时echo "" . json_encode( $data , JSON_PRETTY_PRINT);
网站上没有任何显示
我在用print_r进行while循环后得到的正确输出
{“ 1”:{“ value1”:“ 52”,“ value2”:“ 12”,“ value3”:“ 821”},“ 2”:{“ value1”:“ 52”,“ value2”:“ 12 “}}
答案 0 :(得分:0)
为什么不使用mysqli_fetch_array
:
$query = mysqli_query("SELECT value1, value2, value3 FROM table WHERE id = '$id' LIMIT 10";);
$data = array();
while($row = mysqli_fetch_array($query, MYSQL_ASSOC)){
$data[] = $row;
}
echo json_encode($data, JSON_PRETTY_PRINT);