如何在json_encode()MySQL查询结果中使用该函数?我是否需要遍历行或者是否可以将其应用于整个结果对象?
Pao*_*ino 480
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
函数json_encode需要PHP> = 5.2和PHP-JSON包-如所提到的在这里
注意:mysql自PHP 5.5.0起不推荐使用,请使用mysqli扩展名http://php.net/manual/en/migration55.deprecated.php.
dda*_*ian 43
试试这个,这将正确创建你的对象
$result = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['object_name'][] = $r;
}
print json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
Hug*_*ell 26
http://www.php.net/mysql_query说" mysql_query()返回资源".
http://www.php.net/json_encode表示它可以编码"除资源外"的任何值.
您需要迭代并在数组中收集数据库结果,然后json_encode是数组.
小智 17
谢谢,这对我帮助很大.我的代码:
$sqldata = mysql_query("SELECT * FROM `$table`");
$rows = array();
while($r = mysql_fetch_assoc($sqldata)) {
$rows[] = $r;
}
echo json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
aas*_*ima 11
谢谢......我的回答是:
if ($result->num_rows > 0) {
# code...
$arr = [];
$inc = 0;
while ($row = $result->fetch_assoc()) {
# code...
$jsonArrayObject = (array('lat' => $row["lat"], 'lon' => $row["lon"], 'addr' => $row["address"]));
$arr[$inc] = $jsonArrayObject;
$inc++;
}
$json_array = json_encode($arr);
echo $json_array;
}
else{
echo "0 results";
}
Run Code Online (Sandbox Code Playgroud)
小智 9
根据我的经验,在将数组中的根元素命名为某些内容之前,上述操作无效,在此之前我无法访问最终json中的任何内容.
$sth = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows['root_name'] = $r;
}
print json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
这应该够了吧!
平价
下面的代码在这里工作正常!
<?php
$con=mysqli_connect("localhost",$username,$password,databaseName);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "the query here";
$result = mysqli_query($con,$query);
$rows = array();
while($r = mysqli_fetch_array($result)) {
$rows[] = $r;
}
echo json_encode($rows);
mysqli_close($con);
?>
Run Code Online (Sandbox Code Playgroud)
使用fetchAll()提取所有行作为一个关联数组。
$stmt = $pdo->query('SELECT * FROM article');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
当您的 SQL 有参数时:
$stmt = $pdo->prepare('SELECT * FROM article WHERE id=?');
$stmt->execute([1]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
当您需要重新生成表的密钥时,您可以使用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);
Run Code Online (Sandbox Code Playgroud)
使用fetch_all()提取所有行作为一个关联数组。
$res = $mysqli->query('SELECT * FROM article');
$rows = $res->fetch_all(MYSQLI_ASSOC);
echo json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
当您的 SQL 有参数时,您需要执行准备/绑定/执行/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);
Run Code Online (Sandbox Code Playgroud)
当您需要重新生成表的密钥时,您可以使用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);
Run Code Online (Sandbox Code Playgroud)
请尽快升级到受支持的 PHP 版本!请认真对待。如果您需要使用旧 API 的解决方案,可以这样做:
$res = mysql_query("SELECT * FROM article");
$rows = [];
while ($row = mysql_fetch_assoc($res)) {
$rows[] = $row;
}
echo json_encode($rows);
Run Code Online (Sandbox Code Playgroud)
我的简单解决办法是停止在数字值周围添加语音标记...
while($r = mysql_fetch_assoc($rs)){
while($elm=each($r))
{
if(is_numeric($r[$elm["key"]])){
$r[$elm["key"]]=intval($r[$elm["key"]]);
}
}
$rows[] = $r;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
抱歉,问题过了很久,但是:
$sql = 'SELECT CONCAT("[", GROUP_CONCAT(CONCAT("{username:'",username,"'"), CONCAT(",email:'",email),"'}")), "]")
AS json
FROM users;'
$msl = mysql_query($sql)
print($msl["json"]);
Run Code Online (Sandbox Code Playgroud)
基本上就是:
"SELECT" Select the rows
"CONCAT" Returns the string that results from concatenating (joining) all the arguments
"GROUP_CONCAT" Returns a string with concatenated non-NULL value from a group
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
495051 次 |
| 最近记录: |