Home > Article > Backend Development > Display the implementation code for searching and highlighting keywords in the PHP site
Copy the code The code is as follows:
require_once 'sqlTools.class.php';//Encapsulation class, executable dql, dml statements
$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']."
";
}
?>
Copy the code The code is as follows:
class SqlTools{
private $host="localhost";
private $dbname="test";
private $ dbuser="root";
private $dbpwd="";
private $conn;
public function __construct(){
$this->c
if(!$this->conn){
die("Connect Database failed".mysql_error());
}
mysql_select_db($this->dbname,$this->conn) or die("The database cannot be found".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);
}
}
?>
The above introduces the implementation code for displaying PHP site search and highlighting keywords, including display content. I hope it will be helpful to friends who are interested in PHP tutorials.