Copy Code code as follows:
<?php
Require_once ' sqlTools.class.php ';//Encapsulate class, can execute DQL, DML statements
$info =$_post[' info '];
$sql = "Select Name,password,email from user_500 where name is $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", "<b style=\" color:red\ ">\\1</b>", $row [' name ']);
$row [' Password ']=preg_replace ("/($info)/I", "<b style=\" color:red\ ">\\1</b>", $row [' Password ']);
$row [' Email ']=preg_replace ("/($info)/I", "<b style=\" color:red\ ">\\1</b>", $row [' email ']);
echo $row [' name ']. --> ". $row [' Password ']." --> ". $row [' email ']." <br> ";
}
?>
Thinking Analysis:
When the% $info% contained in the SQL statement is given to the DBMS for execution, he looks for information about the values in the field that contain the variable $info.
% $info---> Find information that ends with a $info value
$info%---> Find information that starts with a value of $info
Preg_replace () The search keyword is highlighted by the regular function (), for example,
$row [' Name ']=preg_replace ("/($info)/I", "<b style=\" color:red\ ">\\1</b>", $row [' name ']);
To replace the value received by the post with the result of a $info (bold red) and to reassign the result to $row[' name '
If you want to search for more than one keyword, you can split the received value $info, such as $info_more=explode ("", $info);//This way can be separated by a space of the keyword segmentation, and then split the result of a query, the same, You can use regular expression functions to replace work to highlight keywords
SqlTools.class.php's source code:
Copy Code code as follows:
<?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 ("Connection 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);
}
}
?>
Original article: Web Development _ Small Fly