Php 搜索结果中的粗体搜索词

Php 搜索结果中的粗体搜索词,php,mysql,search,Php,Mysql,Search,当用户搜索我的数据库时,我希望搜索词在结果中加粗。我似乎找不到一个可以轻松实现到现有代码中的教程或解释。我是PHP新手,请耐心等待。所以,我想要的是,如果有人搜索“蓝色”,那么结果将显示类似的内容: 搜索词:蓝色 结果: 1-蓝色-马-黑色体色-红色眼睛 2-吉米-马-蓝色-体色-黑眼睛 诸如此类。 以下是我的search.php页面代码: <?php $query = $_GET['query']; // gets value sent over search form $m

当用户搜索我的数据库时,我希望搜索词在结果中加粗。我似乎找不到一个可以轻松实现到现有代码中的教程或解释。我是PHP新手,请耐心等待。所以,我想要的是,如果有人搜索“蓝色”,那么结果将显示类似的内容:

搜索词:
蓝色

结果:
1-蓝色-马-黑色体色-红色眼睛
2-吉米-马-蓝色-体色-黑眼睛

诸如此类。

以下是我的search.php页面代码:

<?php
$query = $_GET['query']; 
// gets value sent over search form

$min_length = 3;
// you can set minimum length of the query if you want

if(strlen($query) >= $min_length){ // if query length is more or equal     minimum length then

$query = htmlspecialchars($query); 
// changes characters used in html to their equivalents, for example: < to &gt;

$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT * FROM characters
    WHERE (`name` LIKE '%".$query."%') OR (`player` LIKE '%".$query."%') OR (`dam` LIKE '%".$query."%') OR (`sire` LIKE '%".$query."%') OR (`status` LIKE '%".$query."%')") or die(mysql_error());

// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table

// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

    while($results = mysql_fetch_array($raw_results)){
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

        echo "Searched term:<br>";
echo "<b>" . $query . "</b>";
echo "<br><br>";
echo "Results:<br>";
foreach($results as $index => $resultArray) {
$resultString = $index+1 . " - ";
foreach($resultArray as $key => $value) {
    if (strpos($value, $query) !== FALSE) { // strpos returns a value between 0 and n if the string is found; if it's NOT found, it returns FALSE - due to PHP's veeeerry loose typing, we need to use !== rather than simply !=, because otherwise 0 will return **as** FALSE
        $resultString .= "<b>" . $value . "</b>";
    } else {
        $resultString .= $value;
    }
    $resultString .= " - ";
}
echo substr($resultString, 0, -3) . "<br>"; // We're chopping off the last " - "
}
    }

}
else{ // if there is no matching rows do following
    echo "No results found.";
}

}
else{ // if query length is less than minimum
echo "Search term is invalid.  Minumum search length is: ".$min_length;
}
?>

我有两种方法可以做到这一点。两者都涉及将计划写入屏幕的html保存在变量中一段时间

方法A-在结果集循环时加粗字符串

// In while loop
row = "#".$results['id']." - ";

if $results['name'] == $query {
 row = row."<strong>".$query."</strong>";
else {
 row = row.$query;
}

row = row." - ".$results['breed']." - ".$results['gender'] // SNIP, use the rest like you use it now
//在while循环中
行=“#”。$results['id']。”-“;
如果$results['name']==$query{
行=行。“”$query。“”;
否则{
行=行。$query;
}
行=行。“-”$results['bride'].“-”$results['gender']//SNIP,像现在一样使用其余的
方法B-返回一个字符串,并对搜索词进行查找和替换

html = "";

// while loop
  html = html."#".$results['id']." - ".$results['name']." - ".$results['breed']." - ".$results['gender']." - ".$results['sire']." x ".$results['dam']." - ".$results['genetics']." - ".$results['body']." Base - ".$results['mane']." Mane - ".$results['tail']." Tail - ".$results['eye']." Eyes - ".$results['markings']." - Born: ".$results['birthdate']." - ".$results['bodytype']." Body Type - ".$results['traits']." - ".$results['defects']." - ".$results['extras']." - Achievements: ".$results['achievements']." - Status: ".$results['status']." - Notes: ".$results['notes']." - Played by ".$results['player']."<br><br>";

// end while

html = str_replace(html, $query, "<strong>".$query."</strong>");

echo html;
html=”“;
//while循环
html=html.#“$results['id'].-“$results['name'.]。”-“$results['breed'.]。”-“$results['general'.]”x.“$results['dam'.”-“$results['genetics'.-“$results['body'.]”Base-“$results['mane'..”鬃毛-.“$results['tail'..”尾-.“$results['eye'.['eye'..”尾-.$results['eye'.['eye'.]眼睛-.$results['birth'.['results'.]出生日期].“$result。“-”$results['bodytype']”Body Type-“$results['traits']”-“$results['defects']”-“$results['extrass']”-成就:“$results['satures']”-状态:“$results['Status']”-备注:“$results['Notes']”-由“$results['player']播放。”

”; //结束时 html=str_replace(html,$query,“”,$query.“”; 回声html;
我相信您正在寻找这样的产品:

echo "Searched term:<br>";
echo "<b>" . $query . "</b>";
echo "<br><br>";
echo "Results:<br>";
foreach($results as $index => $resultArray) {
    $resultString = $index+1 . " - ";
    foreach($resultArray as $key => $value) {
        if (strpos($value, $query) !== FALSE) { // strpos returns a value between 0 and n if the string is found; if it's NOT found, it returns FALSE - due to PHP's veeeerry loose typing, we need to use !== rather than simply !=, because otherwise 0 will return **as** FALSE
            $resultString .= "<b>" . $value . "</b>";
        } else {
            $resultString .= $value;
        }
        $resultString .= " - ";
    }
    echo substr($resultString, 0, -3) . "<br>"; // We're chopping off the last " - "
}

我认为它也有点简洁。

我尝试了您的第一个建议,但它返回了一系列错误-我更新了上面的原始帖子,以反映我对代码所做的操作,从而获得了这些错误。谢谢您的帮助。
try {
    $db = new PDO('mysql:host=YOUR HOST; dbname=YOUR DATABASE', 'YOUR USERNAME', 'YOUR PASSWORD');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

try {
    $stmt = $db->prepare("SELECT * FROM CHARACTERS WHERE (name LIKE :query) OR (player LIKE :query) OR (dam LIKE :query) OR (sire LIKE :query) OR (status LIKE :query);")
    $stmt->execute([
        "query" => "%" . $query . "%"; // This binds ":query" to the value that you give it. It takes an array.
    ]);
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}