【问题标题】:DataTable row() is always undefined and can't retrieve data from databaseDataTable row() 始终未定义,无法从数据库中检索数据
【发布时间】:2021-01-25 09:51:10
【问题描述】:

在我的应用程序中,我一直在尝试将display 的所有数据从database 导入DataTable。但我从服务器收到了错误,

dataTable = s {context: Array(1), 选择器: {…}, 表格: ƒ, 表格: ƒ, 绘制:ƒ,…}

HTML 表格是

<table id="user_data">
  <thead>
     <tr>
        <th>Apt ID</th>
        <th>Doctor </th>
        <th>Specialization</th>         
        <th>Patient </th>
        <th>Type</th>
        <th>Apt Date</th>
        <th>Status</th>
        <th>Status but</th>
    </tr>
  </thead>
</table>

这里是Ajax query

$(document).ready(function() {

fetch_data();
function fetch_data()
{
var dataTable = $('#user_data').DataTable({
        "retrieve": true,
        "processing": true,
        "serverSide": true,
        "ajax" : {
     url:"adminquery/fetch/fetch.php",
     method:"POST"
    }
    } );
}
});

fetch.php

<?php

session_start();

include ('../../auth/dbconnection.php');

$columns= array('apt_id','username','specilization','patient_name','type','apt_date','admin_status');

$stmt = $conn->prepare("SELECT * FROM appointment as a,users as u  WHERE a.user_id= u.user_id ORDER BY a.apt_id DESC");

if(isset($_POST["search"]["value"])){

    $stmt .= '
    WHERE apt_id LIKE "%'.$_POST["search"]["value"].'%"
    OR  username LIKE "%'.$_POST["search"]["value"].'%"
    OR  specilization LIKE "%'.$_POST["search"]["value"].'%"
    OR  patient_name LIKE "%'.$_POST["search"]["value"].'%"
    OR  type LIKE "%'.$_POST["search"]["value"].'%"

    OR  apt_date LIKE "%'.$_POST["search"]["value"].'%"
    OR  admin_status LIKE "%'.$_POST["search"]["value"].'%"

    ';
}
if (isset($_POST["order"])) {

    $stmt .= ' ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].'    ';

}else{

    $stmt .= ' ORDER BY apt_id DESC';
}

$query1='';

if ($_POST["length"] != -1) {

    $stmt1 =  'LIMIT '.$_POST['start'] .' , '.$_POST['length'];
}

$number_filter_row= mysqli_num_rows(mysqli_query($conn,$stmt));
$result =mysqli_query($conn,$stmt.$stmt1);

$stmt->execute();
$result = $stmt->get_result();

$data=array();

while($row = $result->fetch_assoc()) {
  
    $sub_array =array();
    $sub_array[] =   $row["apt_id"];
    $sub_array[] =$row["username"];
    $sub_array[] =$row["specilization"] ;
    $sub_array[] =$row["patient_name"] ;
    $sub_array[] =$row["type"] ;
    $sub_array[] = $row["apt_date"];

    if($row["admin_status"]=="0") {  
        $sub_array[] =' <span class="custom-badge status-red">Cancel</span>';
         } else if($row["admin_status"]=="1") {    
            $sub_array[] =' <span class="custom-badge status-green">Active</span>';
             } else {   
                $sub_array[] ='<span class="custom-badge status-blue">Pending</span>';

         } 

$sub_array[] =$row["type"] ;

$data[]=$sub_array;
}

function get($conn)
{

  $stmt = $conn->prepare("SELECT * FROM appointment as a,users as u  WHERE a.user_id= u.user_id ORDER BY a.apt_id DESC");
  $result =mysqli_query($conn,$stmt);
return mysqli_num_rows($result);

}           
$output= array(
    "draw"  => intval($_POST['draw']),
    "recordsTotal"  => get($conn),
    "recordsFiltered"  => $number_filter_row,
    "data"  => $data
    );

    echo json_encode($output);
?>  

我不知道我哪里出错了。有人可以帮助我可能会非常感激。

【问题讨论】:

  • 你确定你的 php(serverside) 正在返回数据吗?我看到你已经准备好了 $stmt = $conn->prepare("SELE.. 并且后来尝试将查询连接到这个?$stmt .= ' ORDER B...
  • 在控制台显示如下
  • @shubham 我哪里出错了
  • 我建议你正确检查你的 php 文件,看看它是否工作正常。表示,如果它返回数据。

标签: jquery mysql ajax datatable datatables


【解决方案1】:

您必须将查询部分附加到 $stmt 中,而不是附加到 $query 中,所以

//$stmt .= ' ORDER BY apt_id DESC'; 
$query .= ' ORDER BY apt_id DESC';

在所有 fetch.php 中更改这些,

然后检查你的html和js代码部分是否和https://datatables.net/examples/data_sources/server_side.html类似

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 2014-04-20
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    相关资源
    最近更新 更多