PHP PDO Datatable Llist json数据列表

时间:2018-09-12 11:16:52

标签: php json pdo

DataTable没有数据

PHP代码

$db = new ...
$statement  = $db->prepare("SELECT * FROM sehirler");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);

echo  json_encode($results);

PHP代码结果

{"ID":"25","Code":"34","City":"Antalya"}

jQuery代码

DataTable链接

https://datatables.net/examples/data_sources/server_side

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            url : "data.php",
            method: "POSt"
        },
    } );
} );

1 个答案:

答案 0 :(得分:1)

provided linkAjax标签上,您可以看到应该从服务器返回的数据格式。

因此,您需要修改脚本的输出:

$db = new ...
$statement  = $db->prepare("SELECT * FROM sehirler");
$statement->execute();
// to get 0-indexed array use `PDO::FETCH_NUM` mode
$results = $statement->fetchAll(PDO::FETCH_ASSOC);  

echo  json_encode(array(
    'data' => $results,
    // Maybe following fields are optional
    //"draw" => 1,
    //"recordsTotal" => 57,
    //"recordsFiltered" =>57,
));