我使用ajax,php和json从mysql数据库中检索信息。
我的代码如图所示。
<?php
$host = "localhost"; //replace with your hostname
$username = "Practical4"; //replace with your username
$password = "1234"; //replace with your password
$db_name = "Practical4"; //replace with your database
$con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = "select * from comment where name='$name'"; //replace emp_info with your table name
$result = mysql_query($sql);
$json = array();
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_row($result)) {
$json['comment'][] = $row;
}
}
mysql_close($con);
echo json_encode($json);
?>
代码工作和输出如下:
{"comment":[["42","this is comment one","National Hotel"],["43","this is comment one","National Hotel"],["44","this is comment two","National Hotel"],["45","this is comment two","National Hotel"]]}
我想重新格式化结果,只能逐行显示每个评论的内容,例如:
this is comment one
this is comment two
知道怎么做吗?
答案 0 :(得分:0)
然后,如果您直接回显注释行或仅捕获数组中的所有注释行并执行该数组的内爆,则不是执行json_encode,而是
<?php
$host="localhost"; //replace with your hostname
$username="Practical4"; //replace with your username
$password="1234"; //replace with your password
$db_name="Practical4"; //replace with your database
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from comment where name='$name'"; //replace emp_info with your table name
$result = mysql_query($sql);
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_row($result)){
//$json['comment'][]=$row;
echo $row[1];
}
}
mysql_close($con);
?>
OR
<?php
$host="localhost"; //replace with your hostname
$username="Practical4"; //replace with your username
$password="1234"; //replace with your password
$db_name="Practical4"; //replace with your database
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from comment where name='$name'"; //replace emp_info with your table name
$result = mysql_query($sql);
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_row($result)){
//$json['comment'][]=$row;
$json[] = $row[1];
}
}
mysql_close($con);
echo implode('<br />', $json);
?>
答案 1 :(得分:0)
如果你没有在ajax电话中提到dataType
success:function(data)
{
data = $.parseJSON(data);
var response;
$.each(data.comment, function(index, value){
response += value+'<br />';
});
$('#your_div_id').html(response);
}
如果您确实指定了dataType='json'
success:function(data)
{
var response;
$.each(data.comment, function(index, value){
response += value+'<br />';
});
$('#your_div_id').html(response);
}
答案 2 :(得分:0)
你可以尝试这个
foreach($json['comment'] as $key=>$arr)
{
echo $arr[1]."<br/>";
}
答案 3 :(得分:0)
您想在哪里进行格式化?
以PHP为例(为什么JSON编码?)
foreach(json_decode($json)->comment as $item)
{
echo $item[1] . PHP_EOL;
}
答案 4 :(得分:0)
检查列是否是您的需求数据列,仅使用键=&gt;存储您所需的数据价值对映射如下:
<?php
$host="localhost"; //replace with your hostname
$username="Practical4"; //replace with your username
$password="1234"; //replace with your password
$db_name="Practical4"; //replace with your database
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from comment where name='$name'"; //replace emp_info with your table name
$result = mysql_query($sql);
$json = array();
if(mysql_num_rows($result)){
while($row=mysql_fetch_row($result)){
if(column_no == 2) // Check the column only for demo
$json['comment']['message']=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>
当您获得ajax响应时,您的JS代码将是(此处数据是您的响应值):
$.each(data.comment, function(index, value){
console.log(value.message)
});