如何在json_encode()
MySQL查询结果中使用该函数?我需要遍历行还是可以将其应用于整个结果对象?
如何在json_encode()
MySQL查询结果中使用该函数?我需要遍历行还是可以将其应用于整个结果对象?
Answers:
$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
函数json_encode
需要PHP> = 5.2和PHP-JSON包-如所提到的在这里
注意:mysql
自PHP 5.5.0起已弃用,请改用mysqli
扩展名http://php.net/manual/en/migration55.deprecated.php。
AS
将列重命名为公共名称,例如SELECT blog_title as title
,这样更干净,并且公众不知道数据库中确切的列是什么。
试试这个,这将正确地创建您的对象
$result = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['object_name'][] = $r;
}
print json_encode($rows);
http://www.php.net/mysql_query说“ mysql_query()
返回资源”。
http://www.php.net/json_encode表示它可以编码“除资源外”的任何值。
您需要遍历遍历并将数据库结果收集在一个数组中,然后在该数组中收集json_encode
。
谢谢,这对我帮助很大。我的代码:
$sqldata = mysql_query("SELECT * FROM `$table`");
$rows = array();
while($r = mysql_fetch_assoc($sqldata)) {
$rows[] = $r;
}
echo json_encode($rows);
谢谢..我的回答是:
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";
}
下面的代码在这里工作正常!
<?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);
?>
抱歉,问题过了很久,但是:
$sql = 'SELECT CONCAT("[", GROUP_CONCAT(CONCAT("{username:'",username,"'"), CONCAT(",email:'",email),"'}")), "]")
AS json
FROM users;'
$msl = mysql_query($sql)
print($msl["json"]);
基本上就是:
"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
GROUP_CONCAT()
受限制group_concat_max_len
。
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','dishant');
$con = mysqli_connect(HOST,USER,PASS,DB);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "select * from demo ";
$sth = mysqli_query($con,$sql);
$rows = array();
while($r = mysqli_fetch_array($sth,MYSQL_ASSOC)) {
$row_array['id'] = $r;
**array_push($rows,$row_array);**
}
echo json_encode($rows);
mysqli_close($con);
?>
aarray_push($ rows,$ row_array); 帮助构建数组,否则它将在while循环中提供最后一个值
这项工作就像追加的方法的StringBuilder中的java
例如$ result = mysql_query(“ SELECT * FROM userprofiles,其中NAME ='TESTUSER'”);
1.)如果$ result仅是一行。
$response = mysql_fetch_array($result);
echo json_encode($response);
2.)如果$ result超过一行。您需要迭代行并将其保存到数组中,并返回其中包含数组的json。
$rows = array();
if (mysql_num_rows($result) > 0) {
while($r = mysql_fetch_assoc($result)) {
$id = $r["USERID"]; //a column name (ex.ID) used to get a value of the single row at at time
$rows[$id] = $r; //save the fetched row and add it to the array.
}
}
echo json_encode($rows);
我有同样的要求。我只想将结果对象打印为JSON格式,所以我使用下面的代码。希望您能在其中找到一些东西。
// Code of Conversion
$query = "SELECT * FROM products;";
$result = mysqli_query($conn , $query);
if ($result) {
echo "</br>"."Results Found";
// Conversion of result object into JSON format
$rows = array();
while($temp = mysqli_fetch_assoc($result)) {
$rows[] = $temp;
}
echo "</br>" . json_encode($rows);
} else {
echo "No Results Found";
}
我这样解决了
$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);
这将作为结果集并通过在javascript部分中使用json parse返回到ajax,如下所示:
obj = JSON.parse(dataX);
$array = array();
$subArray=array();
$sql_results = mysql_query('SELECT * FROM `location`');
while($row = mysql_fetch_array($sql_results))
{
$subArray[location_id]=$row['location']; //location_id is key and $row['location'] is value which come fron database.
$subArray[x]=$row['x'];
$subArray[y]=$row['y'];
$array[] = $subArray ;
}
echo'{"ProductsData":'.json_encode($array).'}';
$rows = json_decode($mysql_result,true);
就如此容易 :-)
$sql = "SELECT JSON_ARRAYAGG(JSON_OBJECT('id', tbl.id)) FROM table tbl WHERE... ";
//And get first row :)
json_encode()
在下面的答案中提供的JSON_NUMERIC_CHECK标志。简单,就像魅力!stackoverflow.com/questions/1390983/...