PHP site search and highlight the key word implementation Code _php tips

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.