The implementation method of the Search keyword in PHP station to lighten
This article mainly introduced the PHP station search keyword to lighten the implementation method, in the case form more detailed analysis of the database table creation and search for the database keyword highlighting implementation skills, very practical value, the need for friends can refer to the next
In this paper, the implementation of search keywords in PHP station is described. Share to everyone for your reference. The specific analysis is as follows:
Our practice is to put the search results out, with the search keyword the same replacement Cheng Gaoliang Word, we will use the Str_replace (you find the keyword, <带有高亮的html标签> you find the keyword, $str);
It's so easy, okay. Let's take a look at an example.
First, create a database: ' Searchkey ';
To create the table again, SQL builds the code as follows:
The code is as follows:
CREATE TABLE ' fangke_cc '. ' Search ' (
' ID ' INT (4) Not NULL auto_increment,
' keyword ' VARCHAR (not NULL),
PRIMARY KEY (' id ')
) ENGINE = MYISAM
We import some data, the SQL build library code is as follows:
The code is as follows:
INSERT into ' search ' (' id ', ' keyword ') VALUES
(1, ' China script first station www.jb51.net '),
(2, ' China script first station www.jb51.net '),
(3, ' China script first station www.jb51.net '),
(4, ' Chinese script programming first station www.jb51.net ');
Okay, let's do the query, the instance code is as follows:
The code is as follows:
if ($_post) {
$db = ' fangke_cc ';
Mysql_pconnect (' localhost ', ' root ', ' root ') or Die (Mysql_error ());
mysql_select_db ($DB);
mysql_query ("Set names ' GBK '");
$key = $_post[' keyword ');
$sql = "SELECT * from search where keyword like '% $key% '";
$query = mysql_query ($sql);
while ($rs = Mysql_fetch_array ($query))
{
Echo Str_replace ($key, "
$key", $rs [' keyword ']), '
';
}
}
?>
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/975879.html www.bkjia.com true http://www.bkjia.com/PHPjc/975879.html techarticle How to implement search keywords in PHP station This article mainly introduces the implementation method of the search keywords in the PHP station, and analyzes the creation of the database table in detail in the instance form .