2015-06-13 45 views
0

摘要/背景

我試圖做一個搜索引擎,可以搜索MySQL中的任何字符。但我遇到以下問題:如何在MySQL中搜索反斜槓()標記和零(0)?

  1. 當我嘗試搜索反斜槓(\)標記時,結果是不需要的。 (結果顯示許多數據不包含反斜槓)
  2. 當我嘗試搜索數字零(0)時,無論內容是什麼,結果都是NONE。

我嘗試

我試圖說明問題。以下是代碼。

<? 
$search="\\";  //The searching word. (Here I wanna search for the word backslash (\).) 
$link=mysql_connect("localhost","xxx","ooo"); 
mysql_select_db("xxx",$link); 

if($search){ 
    $str="select xxx from Sheet where xxx like '%$search%'"; 
    $list=mysql_query($str,$link); 
    while(list($xxx)=mysql_fetch_row($list)){ 
      echo $xxx."<br>"; 
    } 
} 
mysql_close($link); 
?> 

在我的數據庫

下面的數據是在我的數據庫中的數據:(xxx是數據表Sheet領域)

+-------------------------------------------+ 
| xxx          | 
+===========================================+ 
| Happy % ^_^        | 
| Hello! \Hello\       | 
| /\?"'          | 
| [email protected]#$%^&*()_+|}{":?><`1234567890-=\][';/.,| 
| Oh! My God!!!        | 
+-------------------------------------------+ 

按理來說,PHP的頁面應該打印結果如下:

Hello! \Hello\ 
/\?"' 
[email protected]#$%^&*()_+|}{":?><`1234567890-=\][';/., 

但實際上結果是:

Happy % ^_^ 
[email protected]#$%^&*()_+|}{":?><`1234567890-=\][';/., 

爲什麼會發生這樣的?

類似的問題

How to search for slash (\) in MySQL? and why escaping (\) not required for where (=) but for Like is required?


You can change this by specifying another escape character, as in:

SELECT * FROM titles where title LIKE 'test\\' ESCAPE '|'

在這回答者建議增加「ESCAPE‘|’」中的SQL語句的結束,但整個問題不會得到解決。它只是將相同的問題傳遞給另一個字符(|)。那麼有沒有更好的方法來解決問題?

回答

0

正如您可能已經從其他問題得出的那樣Q &因爲反衝是LIKE表達式的轉義關鍵字。但是,您可以使用三個(一些建議的四個)反斜槓來避開它們。

我測試過表達LIKE '%\\\%'如果使用三個反斜槓逃脫他們將匹配輸入'mom\dad'

+0

,你可能會得到這樣的錯誤:'PHP解析錯誤:語法錯誤,意想不到的「localhost」(T_STRING)在第3行的/public_html/test.php' –

+0

然後你必須考慮PHP解析器 – gerrytan

+0

所吃的反斜槓但是結果與上面相同。 它仍然顯示這兩個結果:'快樂%^ _ ^'和 '〜!@#$%^&*()_ + |} {「:?><1234567890 - = \] ['; /。, ' –