jquery ajax使用php json encode获取数据库结果 [英] jquery ajax get database results using php json encode

查看:22
本文介绍了jquery ajax使用php json encode获取数据库结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个使用 jQuery ajax 调用的 php 代码,它查询数据库并获取结果,然后对结果进行 json 编码

I have this php code that is call using jQuery ajax that queries a database and gets results and then json encode the results

//$rows is another query
foreach($rows as $row) {

    $sql = 'SELECT field1, field2 FROM table WHERE field1= :field';

    $stmt = $db->prepare($sql);
    $stmt->bindValue(':field', trim($row['field_1']));
    $stmt->execute();

    $array = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($array);
}

输出的json看起来像这样

The outputting json looks like this

[{"field1":"J1","field2":"0088","field3":"2928868"}][{"field1":"J2","field2":"0171","field3":"2928868"}][{"field1":"J2","field2":"0249","field3":"2928868"}]

我遇到的问题是在 Ajax 响应中处理它.我想做的是遍历每个数组/行并显示数据,但 responseText 显示错误.

The problem I'm getting is processing it in the Ajax response. What I would like to do i s loop through each of the array/rows and display the data but the responseText shows an error.

我认为它应该看起来像这样,但我不确定.

I thought it should look this but i don't know for definite.

[{"field1":"J1","field2":"0088","field3":"2928868"},{"field1":"J2","field2":"0171","field3":"2928868"},{"field1":"J2","field2":"0249","field3":"2928868"}]

我的问题是,我是否正确执行了 json_encode 以及如何输出每一行?

My question is, am i doing the json_encode correctly and how do i output each of the rows?

$.ajax({
    type: "POST",
    url: "check.php",
    data: { order: order },
    dataType: "json",
    cache: false,
    success: function(response) {

        if(response.length != 0) {

            //output results here
        }
        else {
            $('#foo').text('Order number not found!!');
        }

        // set the focus to the order input
        $('#order').focus().val('');
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('An Ajax error was thrown.');
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    }
});

推荐答案

你应该对整个输出进行 JSON 编码,而不是输出每一行的 json 编码版本:

You should JSON encode the entire output, instead of outputting the json encoded version of each row:

$output = array();

//$rows is another query
foreach($rows as $row) {

    $sql = 'SELECT field1, field2 FROM table WHERE field1= :field';

    $stmt = $db->prepare($sql);
    $stmt->bindValue(':field', trim($row['field_1']));
    $stmt->execute();

    $array = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $output[] = $array;
}

echo json_encode($output);

回答您的问题,要在 JavaScript 中处理您的 JSON,您将其视为对象数组.您甚至可以使用 jQuery 通过 $.each 来帮助您遍历结果:

Answering your question, to work with your JSON in JavaScript, you treat it as if it were an array of objects. You can even use jQuery to help loop through the results for you with $.each:

    if(response.length != 0) {

          $.each(response, function(index, row) {

              console.log(row);
              // Access your row variables like so:
              // row.field1, row.field2, row.field3, etc.
          }

    }

如果您更喜欢本机循环,您可以执行以下操作:

If you prefer natively looping through, you can do the following:

    // Let i start at zero. If the response array length is less than i, execute the block, then increment i by 1.
    for(var i = 0; response.length < i; i += 1) {

    }

相关问题/进一步阅读:如何在 JavaScript 中解析 JSON

Related Question / Further Reading: How to parse JSON in JavaScript

这篇关于jquery ajax使用php json encode获取数据库结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆