我如何使用json_encode()函数与MySQL查询结果?我是否需要遍历这些行,或者我可以将其应用到整个结果对象?
当前回答
我是这样解的
$stmt->bind_result($cde,$v_off,$em_nm,$q_id,$v_m);
$list=array();
$i=0;
while ($cresult=$stmt->fetch()){
$list[$i][0]=$cde;
$list[$i][1]=$v_off;
$list[$i][2]=$em_nm;
$list[$i][3]=$q_id;
$list[$i][4]=$v_m;
$i=$i+1;
}
echo json_encode($list);
这将作为结果集返回给ajax 通过使用json解析javascript部分,像这样:
obj = JSON.parse(dataX);
其他回答
使用FOR循环的另一个选项:
$sth = mysql_query("SELECT ...");
for($rows = array(); $row = mysql_fetch_assoc($sth); $rows[] = $row);
print json_encode($rows);
唯一的缺点是循环for比while或特别是foreach慢
根据我的经验,在命名根元素之前,上述方法是行不通的 数组中的东西,我还没有能够访问任何东西 最后的json。
$sth = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows['root_name'] = $r;
}
print json_encode($rows);
这应该能奏效!
使用PDO时
使用fetchAll()获取所有行作为关联数组。
$stmt = $pdo->query('SELECT * FROM article');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
当你的SQL有参数时:
$stmt = $pdo->prepare('SELECT * FROM article WHERE id=?');
$stmt->execute([1]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
当需要重新输入表的键时,可以使用foreach循环并手动构建数组。
$stmt = $pdo->prepare('SELECT * FROM article WHERE id=?');
$stmt->execute([1]);
$rows = [];
foreach ($stmt as $row) {
$rows[] = [
'newID' => $row['id'],
'Description' => $row['text'],
];
}
echo json_encode($rows);
使用mysqli时
使用fetch_all()获取所有行作为关联数组。
$res = $mysqli->query('SELECT * FROM article');
$rows = $res->fetch_all(MYSQLI_ASSOC);
echo json_encode($rows);
当你的SQL有参数时,你需要执行prepare/bind/execute/get_result。
$id = 1;
$stmt = $mysqli->prepare('SELECT * FROM article WHERE id=?');
$stmt->bind_param('s', $id); // binding by reference. Only use variables, not literals
$stmt->execute();
$res = $stmt->get_result(); // returns mysqli_result same as mysqli::query()
$rows = $res->fetch_all(MYSQLI_ASSOC);
echo json_encode($rows);
当需要重新输入表的键时,可以使用foreach循环并手动构建数组。
$stmt = $mysqli->prepare('SELECT * FROM article WHERE id=?');
$stmt->bind_param('s', $id);
$stmt->execute();
$res = $stmt->get_result();
$rows = [];
foreach ($res as $row) {
$rows[] = [
'newID' => $row['id'],
'Description' => $row['text'],
];
}
echo json_encode($rows);
当使用mysql_* API时
请尽快升级到受支持的PHP版本!请认真对待。如果你需要一个使用旧API的解决方案,这是可以做到的:
$res = mysql_query("SELECT * FROM article");
$rows = [];
while ($row = mysql_fetch_assoc($res)) {
$rows[] = $row;
}
echo json_encode($rows);
试试这个,这将正确创建您的对象
$result = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['object_name'][] = $r;
}
print json_encode($rows);
我们不应该在现代应用程序中看到任何mysql_函数的使用,所以使用mysqli_或pdo函数。
显式调用报头("Content-type:application/json");在输出数据之前,一些开发者认为有效负载是最佳实践。这通常不是一个要求,但是向可能接收它的任何对象阐明了有效负载的格式。
假设这是唯一要打印的数据,使用exit()打印json字符串是安全的,它也将终止脚本的执行。同样,这并不是必需的,因为echo也可以很好地工作,但一些开发人员认为显式地终止脚本是一个很好的实践。
MySQLi单行结果集:
exit(json_encode($result->fetch_assoc())); // 1-dimensional / flat
MySQLi多行结果集:
在PHP 8.1.0之前,只能在mysqlnd中使用。
exit(json_encode($result->fetch_all(MYSQLI_ASSOC))); // 2-dimensional / array of rows
MySQLi单行结果集:
$result = $stmt->get_result();
exit(json_encode($result->fetch_assoc())); // 1-dimensional / flat
MySQLi多行结果集
$result = $stmt->get_result();
exit(json_encode($result->fetch_all(MYSQLI_ASSOC))); // 2-dimensional / array of rows
查询结果集对象的PDO单行结果集
exit(json_encode($result->fetch(PDO::FETCH_ASSOC))); // 1-dimensional / flat
查询结果集对象的PDO多行结果集
exit(json_encode($result->fetchAll(PDO::FETCH_ASSOC))); // 2-dimensional / array of rows
预处理语句的PDO单行结果集:
exit(json_encode($stmt->fetch(PDO::FETCH_ASSOC))); // 1-dimensional / flat
预处理语句的PDO多行结果集:
exit(json_encode($stmt->fetchAll(PDO::FETCH_ASSOC))); // 2-dimensional / array of rows
遵守这些规则以防止生成无效json的可能性:
只有在完全完成对结果数组和的操作之后,才应该调用json_encode() 您应该始终使用json_encode()对有效负载进行编码(避免使用其他字符串函数或连接手动生成json字符串)。
如果您需要迭代结果集数据来运行php函数或提供数据库语言不提供的功能,那么您可以立即使用foreach()迭代结果集对象并使用数组语法访问值。
$response = [];
foreach ($result as $row) {
$row['col1'] = someFunction($row['id']);
$response[] = $row;
}
exit(json_encode($response));
如果对数据有效负载调用json_encode(),那么无论有效负载是数组的数组还是对象的数组,都不会有任何区别。创建的json字符串将具有相同的语法。
在完成连接后,不需要显式地关闭数据库连接。当脚本终止时,连接将自动关闭。