[英]Creating a custom WHERE clause for filtering datatables
我正在使用datatables,我正在嘗試創建一個自定義WHERE子句。 我有以下代碼:
$sWhere = "WHERE office=".$varOffice;
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 .= ') AND office='.$varOffice;
}
經過調試,我發現MySQL正在拋出一個錯誤。 錯誤1605。
我決定$sQuery
這里找到的$sQuery
:
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS `".str_replace(" , ", " ", implode("`, `", $aColumns))."`
FROM $sTable
$sWhere
$sOrder
$sLimit
";
然后我發現錯誤是因為WHERE子句沒有引號,如:
SELECT SQL_CALC_FOUND_ROWS `amount_due_owed`, `amount_paid`, `amount_remaining`, `reference_number` FROM accounting WHERE office=Office 1
它應該是:
WHERE office='Office 1'
我一直在嘗試,但我無法弄明白。 我怎么能在變量周圍得到單引號?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.