2011-05-15 95 views
1

我有一个PHP脚本,它可以与jQuery一起工作来提供搜索建议。它从MySQL数据库中提取结果。但是,我只想要5个结果一次显示用户输入的字母,但似乎所有结果都显示出来。为什么会这样呢?PHP搜索建议

我的代码是:

<p id="searchresults"><?php 

$db=new mysqli('localhost','username','password','database'); 

if(isset($_POST['queryString'])){ 
$queryString=$db->real_escape_string($_POST['queryString']); 
      if(strlen($queryString)>0){ 
       $query = $db->query("SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'"); 
       if($query){ 
        while ($result = $query ->fetch_object()){ 
         echo '<a href="/search/'.$result->name.'/1/">';      
         $name=$result->name;    
         echo ''.$name.''; 
        } 
       } 
      } 
     } 
?></p> 

我希望你能明白我试图描述。

+0

那是'real_escape_string'真的SQL注入安全的吗? – 2011-05-15 10:10:58

+2

@Uwe:'real_escape_string'提供了SQL注入安全性。所以,是的。 (虽然PDO更好。) – 2011-05-21 16:00:14

回答

4

变化"SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'"

"SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%' LIMIT 5"

,如果你想将其限制在5个结果。

0

您需要分页代码添加到您的网页:

有示例代码:

<?php 
// Connects to your Database 
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 
mysql_select_db("address") or die(mysql_error()); 
//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 
//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM topsites") or die(mysql_error()); 
$rows = mysql_num_rows($data); 
//This is the number of results displayed per page 
$page_rows = 4; 
//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 
//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 
//This sets the range to display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
//This is your query again, the same one... the only difference is we add $max into it 
$data_p = mysql_query("SELECT * FROM topsites $max") or die(mysql_error()); 
//This is where you display your query results 
while($info = mysql_fetch_array($data_p)) 
{ 
Print $info['Name']; 
echo "<br>"; 
} 
echo "<p>"; 
// This shows the user what page they are on, and the total number of pages 
echo " --Page $pagenum of $last-- <p>"; 
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. 
if ($pagenum == 1) 
{ 
} 
else 
{ 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; 
echo " "; 
$previous = $pagenum-1; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; 
} 
//just a space 
echo " -- "; 
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links  
if ($pagenum == $last) 
{ 
} 
else { 
$next = $pagenum+1; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; 
echo " "; 
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; 
} 
?> 

来源:www.twitter.com/ZishanAdThandar