我正在使用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 = "
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'
我一直在尝试,但我无法弄明白。我怎么能在变量周围得到单引号?