简体   繁体   English

jQuery从MySQL获取数据

[英]JQuery fetch data from Mysql

I have succesfully recetrive data from mysql the code is as follows 我已成功从mysql接收数据,代码如下

alert(data);
$.each(data, function(index, data) {
                        alert(index);
                        alert(data);
                    });

I want to see data.comments. 我想看data.comments。 the message in alert(data) is 警报(数据)中的消息是

[{"visible":"0","comments":"fiat","post_id":"103007636522630"},{"visible":"0","comments":"volvo","post_id":"103007636522630"}] [{“ visible”:“ 0”,“ comments”:“ fiat”,“ post_id”:“ 103007636522630”},{“ visible”:“ 0”,“ comments”:“ volvo”,“ post_id”:“ 103007636522630 “}]

however, in the each function, it shows each character, for example, index=1, data=[, index=2, data={, index=3, data="> 但是,在每个函数中,它显示每个字符,例如index = 1,data = [,index = 2,data = {,index = 3,data =“>

Do anyone know why this happens, I would like to fetch the whole row instead of each character 有谁知道为什么会这样,我想获取整行而不是每个字符

PHP code PHP代码

$data2= $_POST['string'];

$data3= explode(",", $data2);

$data = array();

foreach ($data3 as &$value) {
   $sql = "SELECT * FROM afb_comments where post_id='".$value."'";  
   $result = mysql_query($sql,$conn);
   while($row = mysql_fetch_array($result)){
      $row_data = array(
       'visible' => $row[0], 
       'comments' => $row[1],
       'post_id' => $row[2]
    );
   array_push($data, $row_data);
  }
  }
 echo json_encode($data);

You need to parse your JSON response. 您需要解析JSON响应。

This can be done automatically if you use $.getJSON or if you set the dataType option in your AJAX call like dataType:'json' 如果使用$.getJSON或在AJAX调用中设置dataType选项(例如dataType:'json'则可以自动完成此操作

Or if you want to do it manually: 或者,如果您想手动进行操作:

data = $.parseJSON(data);
$.each(data, function(index, data) {
    alert(index);
    alert(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM