我正在尝试在我的项目中实现 JQuery Bootgrid 以显示来自 MySQL 数据库的数据。我正在查询的数据显示在分页表中,但是当我尝试搜索某些数据时,它向我显示“未找到结果”。我已经在命令行上测试了我的查询,我的查询正常,它是返回我想要的结果。
SELECT * FROM radcheck rc LEFT JOIN radusergroup ON rc.username=radusergroup.username LEFT JOIN userinfo ON rc.username=userinfo.username LEFT JOIN radusergroup disabled ON disabled.username=userinfo.username AND disabled.groupname = 'daloRADIUS-Disabled-Users' WHERE username LIKE 'admin';
我认为“WHERE”子句中存在一些问题,就好像我尝试在上面的查询中使用不带引号的 admin 一样,它会显示一个错误,“WHERE”子句中的未知列“admin” 。我的 PHP 代码中也可能存在同样的问题。请帮助..
fetch.php
<?php
//fetch.php
include("connection.php");
$query = '';
$data = array();
$records_per_page = 10;
$start_from = 0;
$current_page_number = 0;
if(isset($_POST["rowCount"]))
{
$records_per_page = $_POST["rowCount"];
}
else
{
$records_per_page = 10;
}
if(isset($_POST["current"]))
{
$current_page_number = $_POST["current"];
}
else
{
$current_page_number = 1;
}
$start_from = ($current_page_number - 1) * $records_per_page;
$query .= "SELECT * FROM radcheck rc LEFT JOIN radusergroup ON rc.username=radusergroup.username LEFT JOIN userinfo ON rc.username=userinfo.username LEFT JOIN radusergroup disabled ON disabled.username=userinfo.username AND disabled.groupname = 'daloRADIUS-Disabled-Users'";
if(!empty($_POST["searchPhrase"]))
{
$query .= 'WHERE (rc.id LIKE "%'.$_POST["searchPhrase"].'%" ';
$query .= 'OR rc.username LIKE "%'.$_POST["searchPhrase"].'%" ';
$query .= 'OR radusergroup.groupname LIKE "%'.$_POST["searchPhrase"].'%" ';
$query .= 'OR userinfo.firstname LIKE "%'.$_POST["searchPhrase"].'%" ';
}
$order_by = '';
if(isset($_POST["sort"]) && is_array($_POST["sort"]))
{
foreach($_POST["sort"] as $key => $value)
{
$order_by .= " $key $value, ";
}
}
else
{
$query .= 'ORDER BY rc.id DESC ';
}
if($order_by != '')
{
$query .= ' ORDER BY ' . substr($order_by, 0, -2);
}
if($records_per_page != -1)
{
$query .= " LIMIT " . $start_from . ", " . $records_per_page;
}
//echo $query;
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
$query1 = "SELECT * FROM radcheck rc LEFT JOIN radusergroup ON rc.username=radusergroup.username LEFT JOIN userinfo ON rc.username=userinfo.username LEFT JOIN radusergroup disabled ON disabled.username=userinfo.username AND disabled.groupname = 'daloRADIUS-Disabled-Users'";
$result1 = mysqli_query($connection, $query1);
$total_records = mysqli_num_rows($result1);
$output = array(
'current' => intval($_POST["current"]),
'rowCount' => 10,
'total' => intval($total_records),
'rows' => $data
);
echo json_encode($output);
?>
users.php
<?php include ("inc/header.php");?>
<div class="content-wrapper">
<div class="container-fluid">
<!-- Breadcrumbs-->
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#"></a></li>
<li class="breadcrumb-item active">Users</li>
</ol>
</div>
</div>
<div class="container box">
<br />
<div align="left">
<button type="button" id="add_button" data-toggle="modal" data-target="#productModal" class="btn btn-info btn-lg">Add User</button>
</div>
<div class="table-responsive">
<table id="product_data" class="table table-bordered table-striped">
<thead>
<tr>
<th data-column-id="id" data-type="numeric">ID</th>
<th data-column-id="firstname">Customer Name</th>
<th data-column-id="username">User Name</th>
<th data-column-id="value">Password</th>
<th data-column-id="groupname">Groups</th>
<th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
</tr>
</thead>
</table>
</div>
<?php include("inc/footer.php");?>
</div>
</body>
</html>
<?php include("inc/script_modal_user.php")?>
最佳答案
我没有在 WHERE (rc.id 附近的 fetch.php 中关闭括号...我关闭了括号,一切正常..谢谢
if(!empty($_POST["searchPhrase"]))
{
$query .= 'WHERE (rc.id LIKE "%'.$_POST["searchPhrase"].'%" ';
$query .= 'OR rc.username LIKE "%'.$_POST["searchPhrase"].'%" ';
$query .= 'OR radusergroup.groupname LIKE "%'.$_POST["searchPhrase"].'%" ';
$query .= 'OR userinfo.firstname LIKE "%'.$_POST["searchPhrase"].'%" )';
}
关于php - jquery bootgrid 中的搜索结果为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47587780/