I want to only display 5 results from my database with my PHP search script. How can I do this?
我想用PHP搜索脚本只显示我数据库中的5个结果。我怎样才能做到这一点?
My PHP code is:
我的PHP代码是:
<?php
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}%'";
$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);
?>
3 个解决方案
#1
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);
}
?>
-
Solution: Add a
LIMIT
clause to your query, in order to limit the number of the returned results.解决方案:向查询添加LIMIT子句,以限制返回结果的数量。
-
Suggestion: Place your database querying code inside the if clause, because this way you will avoid troubles of the
GLOBAL
variables kind.建议:将数据库查询代码放在if子句中,因为这样可以避免GLOBAL变量类的麻烦。
#2
0
where you have $searchSQL = "SELECT * FROM links WHERE
titleLIKE '%{$query}%'";
you can change this to using a LIMIT clause like so:
你有$ searchSQL =“SELECT * FROM links WHEREtitleLIKE'%{$ query}%'”;您可以将此更改为使用LIMIT子句,如下所示:
$searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 5";
For more information on using a LIMIT clause try the following links:
有关使用LIMIT子句的更多信息,请尝试以下链接:
MySQL参考指南
#3
0
SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 0,5"
that should display 5 results.
应显示5个结果。
#1
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);
}
?>
-
Solution: Add a
LIMIT
clause to your query, in order to limit the number of the returned results.解决方案:向查询添加LIMIT子句,以限制返回结果的数量。
-
Suggestion: Place your database querying code inside the if clause, because this way you will avoid troubles of the
GLOBAL
variables kind.建议:将数据库查询代码放在if子句中,因为这样可以避免GLOBAL变量类的麻烦。
#2
0
where you have $searchSQL = "SELECT * FROM links WHERE
titleLIKE '%{$query}%'";
you can change this to using a LIMIT clause like so:
你有$ searchSQL =“SELECT * FROM links WHEREtitleLIKE'%{$ query}%'”;您可以将此更改为使用LIMIT子句,如下所示:
$searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 5";
For more information on using a LIMIT clause try the following links:
有关使用LIMIT子句的更多信息,请尝试以下链接:
MySQL参考指南
#3
0
SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 0,5"
that should display 5 results.
应显示5个结果。