2011-08-30 74 views
3

我知道這個問題以前已經被問過好幾次了,但是我無法找到我的方式的錯誤。MySQL/PHP like百分號不起作用

問題:mysql_num_rows是返回false結果時

$sql = "SELECT * FROM $topic WHERE $names LIKE '%$q%'"; 

但是,如果我取代以下任何$ sql中,它將返回true。的var_dump

string(8) "Cust_Reg" string(5) "fName" string(2) "j " 
resource(8) of type (mysql result) 
string(46) "SELECT * FROM Cust_Reg WHERE fName LIKE '%j %'" 

$sql = "SELECT * FROM $topic WHERE $names LIKE '%j%'"; 
$sql = "SELECT * FROM $topic WHERE $names ='Jack'"; 
$sql = "SELECT * FROM $topic WHERE $names = '$q' "; 

結果如果我改變$ SQL = 「SELECT * FROM $話題,其中$名稱LIKE '引用%j%'」;

的var_dump爲$行[ 'ID']將顯示

"SELECT * FROM Cust_Reg WHERE fName LIKE '%j%'" string(4) "NjA=" 
string(4) "NjE=" string(4) "NjQ=" string(4) "NjY=" string(4) "ODI=" 

如果你能糾正我在我的方式錯誤,我會apprecaite它。

$q = mysql_real_escape_string($_GET['search']); 
    $q = strtolower($q); 
    $topic = mysql_real_escape_string($_GET['test']); 
    $names = mysql_real_escape_string($_GET['name']); 

    // SELECT * from Account_Reg where Account_Name LIKE '%$q%' 
    $table = "<table style='width:400px; padding:10; display:block;'><tbody>    
       <tr><td>ID</td><td>Account</td><td>First Name</td><td>Email</td></tr>"; 

     $sql = "SELECT * FROM $topic WHERE $names LIKE '%{$q}%'"; 
     $result = mysql_query($sql) or die (mysql_error()); 

     var_dump($topic); 
     var_dump($names); 
     var_dump($q); 
     var_dump($result); 
     var_dump($sql); 

    if(is_resource($result) && mysql_num_rows($result) > 0){ 

     while($row = mysql_fetch_array($result)) {        
      $table .= "<tr><td>".$row['ID']."</td>"; 
      $cryt = base64_encode($row['ID']); 
      $row['ID'] = htmlspecialchars($cryt); 
      $link = "profile.cust.update.php?id=". urlencode($row['ID']); 
      $link = htmlentities($link);  

      if($names == "fName"){  
       $name = $row['fName']; 

      }elseif($names == "Account_Name"){ 
       $name = $row['Account_Name']; 
       $row['email_add'] = "";  
      } 

      $table .="<td></td><td><a href='" .$link ."'</a>" .$name."</td><td>".$row['email_add']."</td></a> </tr>"; 
     }$table .="</tbody</table"; 
    }else{$table = "No row is selected"; } 
+0

'的var_dump($ _GET [ '搜索'])'。它在你當前的測試場景中實際上包含了什麼? – nobody

回答

1

問題是您的$ q "j "中的尾隨空格。當然

"SELECT * FROM $topic WHERE $names LIKE '%$q%'" 

其轉換爲:

"SELECT * FROM $topic WHERE $names LIKE '%j %'" 

返回不同的結果來自:

"SELECT * FROM $topic WHERE $names LIKE '%j%'" 
+0

你是對的! ('_位置:account.search.php?search ='。urlencode($ _ GET ['search'])。'&& test ='。urlencode($ _ GET ['test'])。'&& name ='。urlencode(' $ _GET [ '名稱']));它確實有一個空間...再次感謝Stackoverflow社區! –

4

嘗試使用:

$sql = "SELECT * FROM $topic WHERE $names LIKE '%{$q}%'"; 

$sql = "SELECT * FROM $topic WHERE $names LIKE '%" . $q . "%'"; 

和調試,嘗試輸出$ SQL執行前,怎麼看變量已被替換

echo $sql; die(); 

從文檔210

@AdamWaite:

http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

MySQL提供標準的SQL模式匹配以及圖案的形式匹配基於類似於那些由UNIX實用程序如vi,grep的使用擴展正則表達式,和sed。

通過SQL模式匹配,您可以使用「_」匹配任何單個字符和「%」以匹配任意數量的字符(包括零字符)。在MySQL中,默認情況下,SQL模式不區分大小寫。這裏顯示了一些示例。當您使用SQL模式時,不要使用=或<>;改爲使用LIKE或NOT LIKE比較運算符。

+1

這應該做到這一點。 – Layke

+0

nope,結果仍然是false –

+0

試圖調試查詢?看看它是怎麼樣的,並嘗試通過phpMyAdmin或mysql控制檯執行它 –