php mysql搜索脚本,如何使匹配结果加粗
我下面有一个脚本,它读取网页上 mysql 数据库中显示的信息,请问如何使匹配结果加粗?例如,如果我搜索“john”如何使显示结果为“john bloggs”。谢谢
这是到目前为止的脚本,
<?
mysql_connect ("localhost", "user","pass") or die (mysql_error());
mysql_select_db ("databasename");
$term = $_POST['term'];
$sql = mysql_query("select * from tablename where category like '%$term%' or title like '%$term%' or postcode like '%$term%' or info like '%$term%' ");
while ($row = mysql_fetch_array($sql)){
echo '<br/> Category: '.$row['category'];
echo '<br/> Title: '.$row['title'];
echo '<br/> Address: '.$row['add1'];
echo '<br/> Street: '.$row['street'];
echo '<br/> City: '.$row['city'];
echo '<br/> Postcode: '.$row['postcode'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> E-Mail: '.$row['email'];
echo '<br/> Website: '.$row['website'];
echo '<br/><br/>';
}
?>
I have a script below which reads on displayed information from my mysql database on my webpage, how can I make matched results bold please? for example, if I searched "john" how to make the displayed results "john bloggs". thanks
here is the script so far,
<?
mysql_connect ("localhost", "user","pass") or die (mysql_error());
mysql_select_db ("databasename");
$term = $_POST['term'];
$sql = mysql_query("select * from tablename where category like '%$term%' or title like '%$term%' or postcode like '%$term%' or info like '%$term%' ");
while ($row = mysql_fetch_array($sql)){
echo '<br/> Category: '.$row['category'];
echo '<br/> Title: '.$row['title'];
echo '<br/> Address: '.$row['add1'];
echo '<br/> Street: '.$row['street'];
echo '<br/> City: '.$row['city'];
echo '<br/> Postcode: '.$row['postcode'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> E-Mail: '.$row['email'];
echo '<br/> Website: '.$row['website'];
echo '<br/><br/>';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您有一个数组
$results
,其中包含 MySQL 查询的一些结果。假设您正在
name
字段中搜索。您可以使用非常简单的
str_replace
来实现此目的:这会将
$search
的所有实例(应该是您的搜索字符串)替换为$search< ;/b>
在$result['name']
内。就您而言:
顺便说一句(重要)
您在这里所做的事情:
极其危险。
$_POST['term']
来自用户,如果这个用户填写';DROP TABLE tablename --
会怎样?您的查询将突然更改为删除整个表并删除所有信息的查询。您应该始终检查您的用户输入,这里有一个很好的教程,解释了一些方法:
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
Let's say for instance you have an array
$results
which contains a few results from your MySQL query.Let's say you were searching within the field
name
.You could use a very simple
str_replace
to achieve this:This replaces all instances of
$search
(which should be your search string) with<b>$search</b>
within$result['name']
.In your case:
BY THE WAY (IMPORTANT)
What you are doing here:
Is extremely dangerous.
$_POST['term']
comes from the user, what if this user fills in';DROP TABLE tablename --
? Your query will suddenly change to something that will drop your entire table and delete all your information.You should always check your user input, here is a nice tutorial explaining some methods how:
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
然后对您想要突出显示的每个字段执行此操作。
Then just do this for each field you want to highlight.
您还可以在所有字段中一次性突出显示它。前任。
它需要 PHP 5.3+,但可以重写为 5.2-
You are also able to highlight it in ALL fields one time. ex.
It requires PHP 5.3+ but may be rewriten to 5.2-