search
Homephp教程php手册php站内搜索并高亮显示关键字的实现代码

将sql语句中包含的%$info%交给DBMS执行的时候,他会查找字段中含有变量$info的值的信息

代码如下:
require_once 'sqlTools.class.php';//封装类,可执行dql、dml语句
$info=$_POST['info'];
$sql="select name,password,email from user_500 where name like '%$info%' or password like '%$info%' or email like '%$info%'";
$sqlTools=new SqlTools();
$res=$sqlTools->execute_dql($sql);
while ($row=mysql_fetch_assoc($res)){
$row['name']=preg_replace("/($info)/i","\\1",$row['name']);
$row['password']=preg_replace("/($info)/i","\\1",$row['password']);
$row['email']=preg_replace("/($info)/i","\\1",$row['email']);
echo $row['name']."-->".$row['password']."-->".$row['email']."
";
}
?>

思路分析:
将sql语句中包含的%$info%交给DBMS执行的时候,他会查找字段中含有变量$info的值的信息,
%$info--->查找以$info的值结束的信息
$info%--->查找以$info的值开头的信息
通过正则函数preg_replace()将搜索到的关键字高亮显示,比如,
    $row['name']=preg_replace("/($info)/i","\\1",$row['name']);
    的意思是:通过POST方接收到的值$info替换为加上样式(红色加粗)的结果,并将结果重新赋给$row[‘name']
如果要搜索多个关键字的话,可以对接收到值$info进行分割,比如$info_more=explode(" ",$info);//这种方式能对以空格隔开的关键字进行分割,再对分割后的结果挨个进行查询,同样,可以使用正则表达式函数进行替换工作,以高亮显示关键字
sqlTools.class.php的源代码:
代码如下:
class SqlTools{
private $host="localhost";
private $dbname="test";
private $dbuser="root";
private $dbpwd="";
private $conn;
public function __construct(){
$this->conn=mysql_connect($this->host,$this->dbuser,$this->dbpwd);
if(!$this->conn){
die("连接数据库失败".mysql_error());
}
mysql_select_db($this->dbname,$this->conn) or die("找不到该数据库".mysql_error());
mysql_query("set names utf8");
}
public function execute_dml($sql){
$bool=mysql_query($sql);
if ($bool){
if ($bool>0) {
return 1;
}else{
return 2;
}
}else {
return 0;
}
}
public function execute_dql($sql){
$res=mysql_query($sql);
return $res;
}
public function close_conn(){
mysql_close($this->conn);
}
}
?>

原创文章:WEB开发_小飞
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment