2012-07-19 97 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