【发布时间】:2015-11-10 02:41:31
【问题描述】:
这是我在表(数据表)中搜索的代码: 总记录:333,213 搜索/显示结果的预计时间:5 到 10 秒。
using : ajax: "sample.php", // json datasource
如何快速? 我应该如何修复数据库或我正在使用的代码。
<?php
/* Database connection start */
include ('connectvl.php');
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0=> 'id',
1=> 'FULLNAME',
2=> 'BrgyName',
3=> 'BDAY',
4=> 'RESSTREET'
);
// getting total number records without any search
$sql = "SELECT id";
$sql.=" FROM voterslist2012";
$query=mysqli_query($conn, $sql) or die("cswd_listofpendingprocessgrid.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 id, FULLNAME, BrgyName, BDAY, RESSTREET";
$sql.=" FROM voterslist2012 WHERE 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( id LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR FULLNAME LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR BDAY LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR BrgyName LIKE '".$requestData['search']['value']."%' )";
}
// If there is a search parameter
/*if( !empty($requestData['search']['value']) ) {
$search = mysqli_real_escape_string(
$conn,
// Match beginning of word boundary
"[[:<:]]".
// Replace space characters with regular expression
// to match one or more space characters in the target field
implode("[[.space.]]+",
preg_split("/\s+/",
// Quote regular expression characters
preg_quote(trim($requestData['search']['value']))
)
).
// Match end of word boundary
"[[:>:]]"
);
$sql.=" AND ( id REGEXP '$search' ";
$sql.=" OR FULLNAME REGEXP '$search' ";
$sql.=" OR BrgyName REGEXP '$search' ";
$sql.=" OR BDAY REGEXP '$search' ";
$sql.=" OR RESSTREET REGEXP '$search' )";
}*/
$query=mysqli_query($conn, $sql) or die("cswd_listofpendingprocessgrid.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("cswd_listofpendingprocessgrid.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["id"];
$nestedData[] = $row["FULLNAME"];
$nestedData[] = $row["BDAY"];
$nestedData[] = $row["BrgyName"];
$nestedData[] = $row["RESSTREET"];
$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
?>
【问题讨论】:
标签: mysqli datatables