php - 将 where 子句添加到数据表过滤

标签 php jquery mysql datatables

您好,我正在尝试在我的数据表服务器端处理文件中添加自定义 WHERE 子句。我设法让它在页面加载时工作,但在过滤后它会忽略 where 子句并返回所有结果。

代码:

$sWhere = "WHERE customers_id= ".$customersId;
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
        {
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
        }
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ')';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }
        $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}

我无法弄清楚在哪里添加其他 WHERE 子句以使其在 beign 过滤时也能正常工作。

最佳答案

删除第一行并在末尾添加(在 iffor 之后):

$sWhere .= ($sWhere ? ' AND ' : ' WHERE ') . 'customers_id = ' . $customersId;

关于php - 将 where 子句添加到数据表过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371116/

相关文章:

mysql - 如何加速 max(...) 查询

mysql - mysql 连接到服务器很慢

php - 将单字节字符串转换为双字节字符串

php - PhalconPHP 加入模型 Echo

php - 使用 PHP 和 MySQL 时如何搜索比常规内容更长的内容?

jquery - 在 $ ("#textbox").val() 调用之前解码文本?

php - 对表和元数据进行连接的 MySQL 搜索

php - 严格的标准不允许简单的 php explode 语句?

jquery - 如何更改多个 div id/类以便样式发生变化?

javascript - 如何 "animate"一个元素(元素符号)跟随路径到 jquery 中的另一个元素(目标)