2011-06-25 50 views

回答

2
<?php 

$database=mysql_connect("localhost","username","password"); 
mysql_select_db("database"); 

if(!empty($_GET['q'])){ 
    $query= mysql_real_escape_string(trim($_GET['q'])); 
    $searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 0,5"; 
    //                .^. 
    //          add a limit clause here. | 
    $searchResult = mysql_query($searchSQL); 

    while ($row=mysql_fetch_assoc($searchResult)){ 
     $results[] = "<div class='webresult'><div class='title'><a href='{$row['url']}'>{$row['title']}</a></div><div class='desc'>{$row['description']}</div><div class='url'>{$row['url']}</div></div>"; 
    } 

    echo implode($results); 
} 
?> 
  1. 解決方案:添加LIMIT子句來查詢,以限制返回結果的數量。

  2. 建議:將您的數據庫查詢代碼,如果子句中,因爲這樣你將避免GLOBAL變量樣的煩惱。使用LIMIT子句

    $searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 5"; 
    

    欲瞭解更多信息,請嘗試以下鏈接:

+1

只是爲了澄清。改變是增加「限制0,5」。你可以在這裏找到更多關於限制選擇器的信息http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html – Swift

0

,你必須$searchSQL = "SELECT * FROM links WHERE標題LIKE '%{$query}%'";你可以改變這種使用一個LIMIT子句,像這樣

MySQL Reference guide

0
SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 0,5" 

that should dis播放5結果。