尝试返回值时对象对象错误


object object error when trying to return value

我不太确定我在这里做错了什么,我有一种感觉,这与 json 编码返回有关?我在代码中收到 [对象对象] 返回错误。

这是 PHP:

<?php
require_once('../config.php');
if(isset($_GET['parcel_id'])) {
    $db = new ezSQL_mysql(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    //count number of comments for the id
    $data2 = $db->get_results("select count(*) from comments where parcel_id=" . $_GET['parcel_id']);
    echo json_encode($data2);
}
?>

Jquery/JSON:

        $('#searchTable tr').click(function(){
            var parcel_id = $(this).attr('id');
            $('#ParcelNumber').html(parcel_number);
            var parcel_number = $('td:first', $(this).parents('tr')).text();
            $.ajax({
                url: "classes/get-apn-count-comments.php?parcel_id=" + parcel_id,
                type: "GET",
                data: { parcel_id : parcel_id },
                dataType: 'json',
                error: function(SMLHttpRequest, textStatus, errorThrown){
                    alert("An error has occurred making the request: " + errorThrown);
                },
                success: function(data2){
                    //do stuff here on success
                    //$('#ParcelNumber').html(data[0]["apn"]);
                    $('#ViewComments').html('View ' + data2[0] + ' Comments');
                }
            });
        });
$('#ViewComments').html('View ' + data2[0] + ' Comments');

在上面的行中,data2[0] 实际上是一个包含一个字段(计数)的 json 对象。

1) 在 PHP 中,将查询更改为:

"select count(*) AS count from comments where parcel_id=" . $_GET['parcel_id'] 

(这将使在步骤 2 中更容易引用)

2) 在 jQuery 中,将其更改为:

$('#ViewComments').html('View ' + data2[0].count + ' Comments');

这应该正确引用您要查找的位。