我想讓我們的搜索模塊能夠搜索像* lex這樣的關鍵字。用*代替之前的字母。我發現添加到我的查詢中的%不起作用並返回錯誤。請幫我看看我的代碼。謝謝如何在MYSQL查詢中函數返回值前後添加%
if(substr($searchfirst_name, 0, 1) == '*'){
$subquery .= " AND first_name LIKE %".Formatter::sql($searchfirst_name);
}elseif(substr($searchfirst_name, -1, 1) == '*'){
$subquery .= " AND first_name LIKE ".Formatter::sql($searchfirst_name)."%";
}else{
$subquery .= " AND first_name LIKE ".Formatter::sql($searchfirst_name);
}
我認爲問題是因爲函數Formatter :: sql()。它過濾變量並向其添加斜線。我如何修改或創建一個新的功能來做到這一點?
public static function sql($value, $fieldName = false) {
if($fieldName) {
return '`' . $value . '`';
}
else if(is_string($value)) {
return '\'' . addslashes($value) . '\'';
}
else if($value === null) {
return 'NULL';
}
else if($value === true) {
return 1;
}
else if($value === false) {
return 0;
}
else if(is_numeric($value)) {
return $value;
}
else {
return '\'' . addslashes($value) . '\'';
}
}
你有一個'Formatter :: sql'文檔或它的代碼的鏈接嗎?您需要查看是否有其他參數不能自動包含引號。 – webbiedave
我的猜測是這樣的錯誤:'你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,在'%' – webbiedave