Implementation code for searching and highlighting keywords in PHP site_PHP tutorial

WBOY
Release: 2016-07-21 15:21:54
Original
826 people have browsed it

Copy 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']."
";
}
?>

Idea analysis:
Convert the %$info% contained in the sql statement When handed over to the DBMS for execution, it will search for information containing the value of the variable $info in the field,
%$info---> Search for information ending with the value of $info
$info%--- >Find information starting with the value of $info
Highlight the searched keywords through the regular function preg_replace(), for example,
 $row['name']=preg_replace("/($info )/i","\1",$row['name']);
  It means: the value received through the POST party $ Replace info with the result with style (red bold), and reassign the result to $row['name']
If you want to search for multiple keywords, you can split the received value $info, for example $info_more=explode(" ",$info);//This method can split keywords separated by spaces, and then query the split results one by one. Similarly, you can use regular expression functions for replacement. , to highlight the source code of keyword
sqlTools.class.php:
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->conn=mysql_connect($this->host,$this->dbuser,$this-> ;dbpwd);
if(!$this->conn){
die("Failed to connect to database".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);
}
}
?>

Original article: WEB Development_Xiao Fei

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324825.htmlTechArticleCopy the code as follows: ?php require_once 'sqlTools.class.php';//Encapsulation class, executable dql , dml statement $info=$_POST['info']; $sql="select name,password,email from user_500 where...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!