in$raw\u结果替换空格

ogq8wdun  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(284)

我在我的网站上搜索,我想当查询输入
链接将是 $results['title'] 但它会用-
生产线: web = str_replace(' ','-',$results['title']); 我想要这个:

this-is-a-example.php

以下是完整代码:

$query = $_GET['query'];
$title = $raw_results['title'];
$web = str_replace(' ','-',$results['title']);

$min_length = 1;

if(strlen($query) >= $min_length){ 

    $query = htmlspecialchars($query); 

    $query = mysql_real_escape_string($query);

    $raw_results = mysql_query("SELECT * FROM content WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%') OR (`link` LIKE '%".$query."%') ") or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){

    echo "<a href='https://example.com/".$web.".php'><h3>".$results['title']."</h3></a><br>";

    echo "<p>".$results['title']."</p>";
        }

    }
    else{ 
        echo "No results";
    }

}
else{
    echo "Minimum length is ".$min_length;
}

当我尝试打开链接时,它只运行/.php。
谢谢你的回复

czq61nw1

czq61nw11#

由于查询结果返回多行,因此需要 $web 低于 while 循环。
而且每个文件名应该是小写的,我已经用过了 strtolower 将其转换为小写。
请尝试以下代码:

$query = $_GET['query'];
$title = $raw_results['title'];

$min_length = 1;

if(strlen($query) >= $min_length){ 

    $query = htmlspecialchars($query); 

    $query = mysql_real_escape_string($query);

    $raw_results = mysql_query("SELECT * FROM content WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%') OR (`link` LIKE '%".$query."%') ") or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ 

        while($results = mysql_fetch_array($raw_results)){

            $web = strtolower(str_replace(' ','-',$results['title']));

            echo "<a href='https://example.com/".$web.".php'><h3>".$results['title']."</h3></a><br>";

            echo "<p>".$results['title']."</p>";
        }

    } else { 
        echo "No results";
    }

} else {
    echo "Minimum length is ".$min_length;
}

相关问题