doujiaozhao2489 2015-01-11 04:58
浏览 40

PHP为Javascript生成了JSON - 两个带有硬编码数组的脚本可以工作,另一个mysql生成的数组不起作用

PHP generated array then json_encode as output Javascript. I have two php script,

the one with hard coded array works and the other one with mysql generated array does not work. Your help in making the mysql one work will be highly appreciated.

PHP that works, the JSON passed to javascript which displays JSON object correctly.

$arr = array(
    array(
        "first_name" => "Darian",
        "last_name" => "Brown",
        "age" => "28",
        "email" => "darianbr@example.com"
    ),
    array(
        "first_name" => "John",
        "last_name" => "Doe",
        "age" => "47",
        "email" => "john_doe@example.com"
    )
);


echo json_encode($arr);
?>

php with mysql generated array NOT work, javascript cannot display the json object.

$result_event = mysqli_query($bd, $qry);

$json = array();

while ($r = mysqli_fetch_assoc($result_event)) {
    $json[] = array("first_name" => $r['first_name'], "last_name" => $r['last_name'], "age" => $r['age'], "email" => $r['email']);

}

echo json_encode($json);
mysqli_close($bd);

Here's the Javascript:

<script type='text/javascript'>
            $(document).ready(function () {
                /* call the php that has the php array which is json_encoded */
                $.getJSON('json_encoded_array.php', function(data) { 
                //$.getJSON('getEventJSON.php', function (data) {
                    /* data will hold the php array as a javascript object */
                    $.each(data, function (key, val) {
                        $('ul').append('<li id="' + key + '">' + val.first_name + ' ' + val.last_name + ' ' + val.email + ' ' + val.age + '</li>'); 
                        //$('ul').append('<li id="' + key + '">' + val.active + ' ' + val.event_id + '</li>');
                });
            });
       });
    </script>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题