2012-07-19 96 views
0

我有這樣的代碼:搜索查詢將返回一個空的結果集

PHP:

[CONNECT TO DATABASE OR DIE (THIS WORKS)] 
if(isset($_GET['search'])) // If it's submitted 
    { 
    $inp = Clean($_GET['inpname']); // Clean my input 
    $sQuery="SELECT name FROM user WHERE (id LIKE '%$inp%')"; // mySql query 
    $r = mysql_query($sQuery) or die(mysql_error()); 
    if(mysql_affected_rows()===0) // If no match found 
     echo "{$inp} is not in our database."; 
    else 
     { 
     echo "<p>{$inp} was successfully searched.</p>"; // Yes, the query worked 
     while($row=mysql_fetch_array($r)) // Loop through the query results 
      echo "{$row[0]}<br>"; // Show the results 
     } // End of the else statement 
    } // End of the if statement 

function Clean($str) // Clean my input 
{ 
    return mysql_real_escape_string(strip_tags(trim($sStr))); // Remove traces of injection 
} 

HTML:

<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get"> 
<input name="inpname" type="text"> 
<input type="submit" name="search" value="Search"> 
</form> 

在我的數據庫我想搜索的成員由ID,但是當我搜索例如1我沒有結果:不在我們的數據庫中。 (/index.php?inpname=1 &搜索=搜索)

如果我使用'%'。$ inp。「%'而不是'%$ inp%',這是行得通的,但它顯示我的數據庫中的所有用戶。

+0

什麼是從'預期$ _GET ['inpname']'一個int或一個字符串,例如一個名字,你的腳本也容易受到xss和PDO的攻擊,或者mysqli應該被用於mysql_ * fun ctions。 – 2012-07-19 07:18:40

+0

任何建議使用什麼? – 2012-07-19 07:24:01

+0

你的乾淨的函數有一個錯誤'修剪($ sStr)' - ** $ sStr **當它應該只是'$ str' – 2012-07-19 07:27:50

回答

1

如果檢查PHP手冊說:

int mysql_affected_rows ([ resource $link_identifier = NULL ]) 

由最後插入獲取受影響的行數,更新,替換或刪除與相關查詢link_identifier。

http://php.net/manual/en/function.mysql-affected-rows.php

所以,如果你做SELECT查詢沒有返回值。

更改代碼這樣的事情

if(isset($_GET['search'])) // If it's submitted 
{ 
    $inp = Clean($_GET['inpname']); // Clean my input 
    $sQuery="SELECT name FROM user WHERE id LIKE '%$inp%'"; // mySql query 
    $r = mysql_query($sQuery) or die(mysql_error()); 

    if (mysql_num_rows($r)) 
    { 
     while ($row=mysql_fetch_array($r)) 
     { 
      echo $row['name']."<br>"; // Show the results 
     } 

    } 
    else 
    { 
     echo "{$inp} is not in our database."; 
    } 

} // End of the if statement 

如果你想只有一排

if(isset($_GET['search'])) // If it's submitted 
{ 
    $inp = Clean($_GET['inpname']); // Clean my input 
    $sQuery="SELECT name FROM user WHERE id LIKE '%$inp%' LIMIT 1"; // mySql query 
    $r = mysql_query($sQuery) or die(mysql_error()); 

    if (mysql_num_rows($r)) 
    { 
     $row=mysql_fetch_array($r) 
     echo $row['name']."<br>"; // Show the results 
    } 
    else 
    { 
     echo "{$inp} is not in our database."; 
    } 

} // End of the if statement 

,如果你想通過精確ID搜索

if(isset($_GET['search'])) // If it's submitted 
{ 
    $inp = Clean($_GET['inpname']); // Clean my input 
    $sQuery="SELECT name FROM user WHERE id = $inp LIMIT 1"; // mySql query 
    $r = mysql_query($sQuery) or die(mysql_error()); 

    if (mysql_num_rows($r)) 
    { 
     $row=mysql_fetch_array($r) 
     echo $row['name']."<br>"; // Show the results 
    } 
    else 
    { 
     echo "{$inp} is not in our database."; 
    } 

} // End of the if statement 
+0

不好。我可以搜索任何ID只顯示第一個帳戶! – 2012-07-19 07:26:41

+0

我在你看到的那一刻改變了這個:) – Sangar82 2012-07-19 07:32:02

+0

這很糟糕,因爲你發佈的fisrt腳本沒問題,現在當我搜索1時,我看到包含1的所有帳戶。我只需要獨特的搜索。 - 修正:'$ inp':) – 2012-07-19 07:37:35

0

我發現,與LIKE搜索將工作最好的,如果你這樣做:

id LIKE '%$inp%' OR id LIKE '%$inp' OR id LIKE '$inp%' OR id = '$inp' 

因爲%意味着必須有至少一個符號。因此%1%找不到1,但會找到417。至少,這是什麼工作對我來說:)

+0

http://www.123autotraffic.com/search_user/index.php?inpname=1&search =搜索顯示我的所有用戶。 – 2012-07-19 07:21:35