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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度