简体   繁体   English

从mysql和php检索json数据到jquery

[英]retrieving json data from mysql and php into jquery

I am trying to bring mysql data into jquery by using php, I get the data into a JSON format like this. 我正在尝试使用php将mysql数据带入jquery,我将数据转换为JSON格式,如下所示。

{"uid":"33","title":"Apple, Peach, Grapefruit","ing1":"apple","qty1":"1","meas1":"whole","ing2":"peaches \/ halved and","qty2":"2","meas2":"each","ing3":"grapefruit \/ peeled","qty3":"2","meas3":"each","ing4":"","qty4":"0","meas4":"each","ing5":"","qty5":"0","meas5":"each","ing6":"","qty6":"0","meas6":"each","ing7":"","qty7":"0","meas7":"each","ing8":"","qty8":"0","meas8":"each","ing9":"","qty9":"0","meas9":"each","ing10":"","qty10":"0","meas10":"each","servings":"2","benefits":""}

using this following code: 使用以下代码:

require_once'connect.php'; 
$uid = $_GET['uid'];
$sql = "SELECT * FROM recipes WHERE uid = '$uid'";
$result = mysqli_query($conn, $sql);

$num_rows = mysqli_affected_rows($conn);
while($row = mysqli_fetch_assoc($result)) {
$data = json_encode($row);
echo $data;
}

I am using jquery .get to pull it into the web page with this code. 我正在使用jquery .get使用此代码将其拉入网页。

$(document).ready(function(e) {
var id = location.search;
uid=id.substring(4);
$.get('../jqm_juicing/data/get_json.php?uid=' + uid,function(data, status){
$("#display").append(data);
});
});

It displays the json data as above. 它显示上面的json数据。 I would like to be able to access the different elements individually, how do I do that? 我希望能够分别访问不同的元素,该怎么做?

In your jQuery, add JSON.parse() before you append (data). 在您的jQuery中,在添加(数据)之前添加JSON.parse()。

$(document).ready(function(e) {
    var id = location.search;
    uid=id.substring(4);
    $.get('../jqm_juicing/data/get_json.php?uid=' + uid,function(data, status){
        $("#display").append(JSON.parse(data));
    });
});

I just changed this line: 我刚刚更改了这一行:

$("#display").append(JSON.parse(data));

Hope this helps! 希望这可以帮助!

If you wrap it with <script> tags, and declare it as a variable, you will have it. 如果用<script>标记包装它,并将其声明为变量,则将拥有它。

require_once'connect.php'; 
$uid = $_GET['uid'];
$sql = "SELECT * FROM recipes WHERE uid = '$uid'";
$result = mysqli_query($conn, $sql);

$num_rows = mysqli_affected_rows($conn);

echo "<script>";
while($row = mysqli_fetch_assoc($result)) {
  $data = json_encode($row);
  echo "var myJson=" . $data;
}
echo "</script>";

Now in your main page scripts, you can access this JSON like any other normal JSON. 现在,在主页脚本中,您可以像访问其他任何普通JSON一样访问此JSON。
Here is an interval that you can place anywhere in your main page scripts: 您可以在主页脚本中的任意位置放置一个间隔:
It is just to test this ;) 只是为了测试;)

var checkJson = setInterval(function(){

  if(typeof(myJson.uid)!="undefined"){

    clearInterval(checkJson);

    console.log("myJson uid: " + myJson.uid);
    console.log("myJson title: " + myJson.title);
    // etc...

  }else{
    console.log("JSON not loaded yet.");
  }

},500);

Retrieve the json data to use json response jquery parse function. 检索json数据以使用json响应jquery解析功能。 json data using this function jQuery.parseJSON() 使用此功能的json数据jQuery.parseJSON()

http://api.jquery.com/jquery.parsejson/ http://api.jquery.com/jquery.parsejson/

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

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