Php site to search and highlight the Keyword implementation code. Copy the code as follows :? Phprequire_oncesqlTools.class.php; encapsulation class, which can execute dql and dml statements $ info $ _ POST [info]; $ sqlselectname, password, emailfromuser_500where
The code is as follows:
Require_once 'sqltools. class. php'; // encapsulation class, which can execute dql and 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 ']."
";
}
?>
Train of Thought analysis:
When the SQL statement contains % $ info % and is handed over to the DBMS for execution, it will find information about the value of $ info in the field,
% $ Info ---> search for information ending with $ info
$ Info % ---> search for information starting with $ info
Use the regular function preg_replace () to highlight the searched keywords, for example,
$ Row ['name'] = preg_replace ("/($ info)/I ","
\ 1", $ Row ['name']);
The value $ info received by the POST Party is replaced with the result of adding the style (red bold), and the result is re-assigned to $ row ['name']
To search for multiple keywords, you can split the received value $ info, for example, $ info_more = explode ("", $ info ); // This method can be used to split keywords separated by spaces, and then query the split results one by one. Similarly, you can use a regular expression function to replace the results to highlight keywords.
Source code of sqlTools. class. php:
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 ("this 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 _ Xiaofei
The http://www.bkjia.com/PHPjc/324825.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324825.htmlTechArticle code is as follows :? Php require_once 'sqltools. class. php '; // encapsulation class, which can execute dql and dml statements $ info = $ _ POST ['info']; $ SQL = "select name, password, email from user_500 where...