亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP實(shí)現(xiàn)多個(gè)關(guān)鍵詞搜索查詢功能示例

 更新時(shí)間:2018年06月27日 09:27:19   作者:codeweblog  
這篇文章主要介紹了PHP實(shí)現(xiàn)多個(gè)關(guān)鍵詞搜索查詢功能,結(jié)合實(shí)例形式分析了php使用mysql中l(wèi)ike、union等語句模糊查詢相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP實(shí)現(xiàn)多個(gè)關(guān)鍵詞搜索查詢功能。分享給大家供大家參考,具體如下:

PHP對于數(shù)據(jù)庫的搜索主要通過使用SQL語句中的like子句來實(shí)現(xiàn)。如果同時(shí)搜索多個(gè)關(guān)鍵詞,可以使用union子句來將搜索結(jié)果合并起來。以下代碼實(shí)現(xiàn)了一個(gè)搜索頁面。

引用

<?php require_once(''Connections/conn.php''); ?>
<?php
$colname_rs = $_GET[''key'']; //獲得用戶輸入
$result = explode('','',$_GET[''key'']);//分解用戶輸入的多個(gè)關(guān)鍵詞,存入$result數(shù)組
mysql_select_db($database_conn, $conn); //連接數(shù)據(jù)庫
//根據(jù)多個(gè)關(guān)鍵詞構(gòu)建SQL語句
$query_rs = "SELECT * FROM (";
for($i=0;$i<count($result);$i++) //根據(jù)每個(gè)搜索關(guān)鍵詞構(gòu)建SQL語句
{
if($i==0) //對第一個(gè)關(guān)鍵詞,不使用UNION
$query_rs .= "SELECT * FROM searchtable WHERE title LIKE ''%$result[0]%''
OR content LIKE ''%$result[0]%''";
else //對其他關(guān)鍵詞,使用UNION連接
$query_rs .= " UNION SELECT * FROM searchtable WHERE title LIKE
''%$result[$i]%'' OR content LIKE ''%$result[$i]%''";
}
$query_rs .= ") T ORDER BY last_access DESC"; //對搜索結(jié)果排序
//執(zhí)行SQL語句
$rs = mysql_query($query_rs, $conn) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);
?>
<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="get" action="?">
<div align="center">請輸入要搜索關(guān)鍵詞:
<input name="key" type="text" size="64" value="<?php echo $_GET[''key''] ?>">
<input type="submit" value="Submit">
</div>
</form>
<p align="center"><B>當(dāng)前關(guān)鍵詞:
<?php
for($i=0;$i<count($result);$i++) { //循環(huán)顯示關(guān)鍵詞
echo $result[$i]." ";
}
?></B></p>
<p><hr></p>
<?php if($totalRows_rs>0) do { //顯示當(dāng)前搜索結(jié)果 ?>
<p>* <a href="show.php?key=<?php echo $colname_rs ?>&id=<?php echo
$row_rs[''id'']; ?>"><?php echo $row_rs[''title'']; ?></a>(<?php echo
$row_rs[''click'']; ?> | <?php echo $row_rs[''last_access'']; ?>)</p>
<?php } while ($row_rs = mysql_fetch_assoc($rs)); ?>
</body>
</html>
<?php
mysql_free_result($rs);
?>

這里,在頁面上可以使用英文逗號“,”來實(shí)現(xiàn)多個(gè)關(guān)鍵詞的搜索。

這里,仍然將與數(shù)據(jù)庫的連接放到一個(gè)專門的PHP文件中以方便后期的修改。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論