Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php json编码的格式输出_Php_Mysql_Json - Fatal编程技术网

Php json编码的格式输出

Php json编码的格式输出,php,mysql,json,Php,Mysql,Json,我使用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

我使用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); 

?>
我想将结果重新格式化为只逐行显示每个注释的内容,如:

this is comment one
this is comment two

你知道怎么做吗?

那么,与其做json_编码,不如直接回显注释行,或者只在一个数组中捕获所有注释行,然后对该数组执行内爆,如下所示

<?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);

?> 


如果在ajax调用中没有提到
数据类型

success:function(data)
{
        data = $.parseJSON(data);
        var response;
        $.each(data.comment, function(index, value){
             response += value+'<br />';
        });
        $('#your_div_id').html(response);
}
你可以试试这个

foreach($json['comment'] as $key=>$arr)
{
   echo $arr[1]."<br/>";
}
foreach($json['comment']as$key=>$arr)
{
echo$arr[1]。“
”; }
您想在哪里进行格式化

以PHP为例(为什么使用JSON编码?)


检查该列是否为require data列,使用key=>value对映射仅存储所需数据,如下所示:

{"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"]]}
<?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); 

?> 

等等..应该在哪里重新格式化?在php页面中,还是在发送ajax请求的页面中?
foreach($json['comment'] as $key=>$arr)
{
   echo $arr[1]."<br/>";
}
foreach(json_decode($json)->comment as $item)
{
    echo $item[1] . PHP_EOL;
}
<?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); 

?> 
    $.each(data.comment, function(index, value){
         console.log(value.message)
    });