我使用的这段代码工作得很好但是当我尝试用where子句查询时,看起来它不起作用,它仍然显示所有数据,虽然我已经放了一个条件。我也尝试改变我放置where子句的方式,但它说"没有在服务器中找到数据"所以我把代码放回原来的形式。我也尝试过一个特定的where子句,例如"其中client =' eadept'"但它仍然显示所有细节。这是我的代码。
session_start();
/* Database connection start */
$servername = "localhost";
$item = "root";
$password = "";
$dbname = "sample";
$conn = mysqli_connect($servername, $item, $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 => 'facility',
1 => 'title',
2 => 'start',
3 => 'end',
4 => 'fee',
5 => 'status'
);
$uname = ($_SESSION['username']);
// getting total number records without any search
$sql = "SELECT facility, title, start, end, fee, status ";
$sql .= " FROM reservation WHERE client = '$uname'";
$query = mysqli_query($conn, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT facility, title, start, end, fee, status ";
$sql .= " FROM reservation WHERE 1=1";
if(!empty($requestData['search']['value'])) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql .= " AND ( facility LIKE '" . $requestData['search']['value'] . "%' ";
$sql .= " OR title LIKE '" . $requestData['search']['value'] . "%' ";
$sql .= " OR fee LIKE '" . $requestData['search']['value'] . "%' )";
}
$query = mysqli_query($conn, $sql);
$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'] . " ";
// $sql.="WHERE client = 'eadept'";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query = mysqli_query($conn, $sql);
$data = array();
while($row = mysqli_fetch_array($query)) { // preparing an array
$nestedData = array();
$nestedData[] = $row["facility"];
$nestedData[] = $row["title"];
$nestedData[] = $row["start"];
$nestedData[] = $row["end"];
$nestedData[] = $row["fee"];
$nestedData[] = $row["status"];
$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
我指的是数组下的查询。任何人都可以请求帮助,告诉我们要改变什么,这样才有用吗?
答案 0 :(得分:0)
我注意到我只是把查询放在错误的地方。这是执行WHERE子句的正确代码。
$uname = ($_SESSION['username']);
// getting total number records without any search
$sql = "SELECT facility, item, start, end, fee, status ";
$sql.=" FROM reservation";
$query=mysqli_query($conn, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT facility, item, start, end, fee, status ";
$sql.=" FROM reservation WHERE client = '$uname'";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( facility LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR item LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR fee LIKE '".$requestData['search']['value']."%' )";
// $sql.=" OR item LIKE '".$requestData['search']['value']."%' )";
// $sql.=" OR start LIKE '".$requestData['search']['value']."%' )";
// $sql.=" OR end LIKE '".$requestData['search']['value']."%' )";
// $sql.=" OR fee LIKE '".$requestData['search']['value']."%' )";
// $sql.=" OR status LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql);
$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']." ";
// $sql.="WHERE client = 'eadept'";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql);