php mysql搜索脚本,如何使匹配结果加粗

发布于 2024-12-03 04:25:27 字数 958 浏览 2 评论 0原文

我下面有一个脚本,它读取网页上 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金橙橙 2024-12-10 04:25:27

假设您有一个数组 $results,其中包含 MySQL 查询的一些结果。

假设您正在name 字段中搜索。

您可以使用非常简单的 str_replace 来实现此目的:

foreach($results as $result)
    echo str_replace($search,'<b>'.$search.'</b>',$result['name']);

这会将 $search 的所有实例(应该是您的搜索字符串)替换为 $search< ;/b>$result['name'] 内。

就您而言:

while ($row = mysql_fetch_array($sql)){

    echo '<br/> Category: '.str_replace($term,'<b>'.$term.'</b>',$row['category']);
    echo '<br/> Title:    '.str_replace($term,'<b>'.$term.'</b>',$row['title']);
    echo '<br/> Address:  '.$row['add1'];
    echo '<br/> Street:   '.$row['street'];
    echo '<br/> City:     '.$row['city'];
    echo '<br/> Postcode: '.str_replace($term,'<b>'.$term.'</b>',$row['postcode']);
    echo '<br/> Phone:    '.$row['phone'];
    echo '<br/> E-Mail:   '.$row['email'];
    echo '<br/> Website:  '.$row['website'];
    echo '<br/><br/>';

}

顺便说一句(重要)

您在这里所做的事情:

$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%' ");

极其危险。 $_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:

foreach($results as $result)
    echo str_replace($search,'<b>'.$search.'</b>',$result['name']);

This replaces all instances of $search (which should be your search string) with <b>$search</b> within $result['name'].

In your case:

while ($row = mysql_fetch_array($sql)){

    echo '<br/> Category: '.str_replace($term,'<b>'.$term.'</b>',$row['category']);
    echo '<br/> Title:    '.str_replace($term,'<b>'.$term.'</b>',$row['title']);
    echo '<br/> Address:  '.$row['add1'];
    echo '<br/> Street:   '.$row['street'];
    echo '<br/> City:     '.$row['city'];
    echo '<br/> Postcode: '.str_replace($term,'<b>'.$term.'</b>',$row['postcode']);
    echo '<br/> Phone:    '.$row['phone'];
    echo '<br/> E-Mail:   '.$row['email'];
    echo '<br/> Website:  '.$row['website'];
    echo '<br/><br/>';

}

BY THE WAY (IMPORTANT)

What you are doing here:

$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%' ");

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

那片花海 2024-12-10 04:25:27
function highlight($term, $result) {
    return str_replace($term, '<strong>'.$term.'</strong>', $result);
}

然后对您想要突出显示的每个字段执行此操作。

while ($row = mysql_fetch_array($sql)){
echo '<br/> Category: '.highlight($term, $row['category']);
echo '<br/> Title: '.highlight($term, $row['title']);
echo '<br/> Address: '.$row['add1'];
echo '<br/> Street: '.$row['street'];
echo '<br/> City: '.$row['city'];
echo '<br/> Postcode: '.highlight($term, $row['postcode']);
echo '<br/> Phone: '.$row['phone'];
echo '<br/> E-Mail: '.$row['email'];
echo '<br/> Website: '.$row['website'];
echo '<br/><br/>';
}
function highlight($term, $result) {
    return str_replace($term, '<strong>'.$term.'</strong>', $result);
}

Then just do this for each field you want to highlight.

while ($row = mysql_fetch_array($sql)){
echo '<br/> Category: '.highlight($term, $row['category']);
echo '<br/> Title: '.highlight($term, $row['title']);
echo '<br/> Address: '.$row['add1'];
echo '<br/> Street: '.$row['street'];
echo '<br/> City: '.$row['city'];
echo '<br/> Postcode: '.highlight($term, $row['postcode']);
echo '<br/> Phone: '.$row['phone'];
echo '<br/> E-Mail: '.$row['email'];
echo '<br/> Website: '.$row['website'];
echo '<br/><br/>';
}
征棹 2024-12-10 04:25:27

您还可以在所有字段中一次性突出显示它。前任。

while ($row = array_map(function($res) use ($search){
        str_replace($search,'<b>'.$search.'</b>',$res);
    },mysql_fetch_array($sql))){
    //your print
}

它需要 PHP 5.3+,但可以重写为 5.2-

You are also able to highlight it in ALL fields one time. ex.

while ($row = array_map(function($res) use ($search){
        str_replace($search,'<b>'.$search.'</b>',$res);
    },mysql_fetch_array($sql))){
    //your print
}

It requires PHP 5.3+ but may be rewriten to 5.2-

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文