php - DataTables 服务器端处理警告

标签 php mysql arrays sql-server datatables

我正在使用 DataTables,但它导致了错误,并且我找不到错误所在。

我想使用服务器端处理,因为插入 11k 行后,它是滞后的。

Error

我正在使用的代码:

<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "accounts";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* Database connection end */


// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$columns = array( 
// datatable column index  => database column name
    0 =>'type', 
    1 => 'country',
    2 => 'information',
    3 => 'seller',
    4 => 'price'
);

// getting total number records without any search
$sql = "SELECT type, country, information, seller, price ";
$sql.=" FROM accounts WHERE type2='1'";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT type, country, information, seller, price ";
$sql.=" FROM accounts WHERE type2='1' AND 1=1";
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
    $sql.=" AND ( type LIKE '".$requestData['search']['value']."%' ";    
    $sql.=" OR country LIKE '".$requestData['search']['value']."%' ";

    $sql.=" OR information LIKE '".$requestData['search']['value']."%' )";

        $sql.=" OR seller LIKE '".$requestData['search']['value']."%' )";

}
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */    
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array(); 

    $nestedData[] = $row["type"];
    $nestedData[] = $row["country"];
    $nestedData[] = $row["information"];
        $nestedData[] = $row["seller"];
    $nestedData[] = $row["price"];


    $data[] = $nestedData;
}



$json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );

echo json_encode($json_data);  // send data as json format

?>

我没有找到任何解决方案。

PHP 文件中显示的错误

Image

最佳答案

$requestData 没有开始键和顺序键,因此请使用 isset()empty() 函数检查它们是否没有空,然后只使用它们。

例如:

LIMIT " . (empty($requestData['start']) ?
    'whatever you want to put default value' :
    $requestData['start'])

关于php - DataTables 服务器端处理警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323214/

相关文章:

php - 显示最受好评的 PHP 和 MySQL

php - 如何诊断或降低网页内容的 "waiting"时间?

MySQL 查询用该表中的其他数据填充该表

c# - MySql.Data.MySqlClient.MySqlException (0x80004005) 异常

javascript - 如何在 javascript 中将子数组字符串数组转换为子数组数组?

php - 如何将一对多连接查询的结果减少为每个 "one"的单个对象?

javascript - 通过Javascript函数返回PHP数据,无需提交表单

mysql - 当子查询在 ON 条件下使用父查询的值时,会产生 'Unknown column' 错误

c++ - 当我在结构中包含多个数组时,我的程序跳过了一堆代码

sql - 动态转换数组元素的类型以匹配 PostgreSQL 查询中的某些表达式类型