0
因此,我目前正在創建一個搜索引擎剛出來的興趣,並好奇,如果我能夠查詢不僅僅是在MySQL數據庫中的關鍵字列。目前,我可以搜索某些東西,並根據我的「關鍵字」列獲得結果。但是,如果我所搜索的字詞或短語不在關鍵字列中,而是在標題列或描述列中找到,它是否有可能顯示,因爲它在這些列中發現了它?mysql php搜索引擎查詢多列
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Search Engine - Search</title>
</head>
<body>
<h2>Search Engine</h2>
<form action='./search.php' method='get'>
<input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' />
<input type='submit' value='Search'>
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM databeast WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("localhost", "username", "password");
mysql_select_db("fapster") or die(mysql_error());
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)){
$url = $row['url'];
$title = $row['title'];
$keywords = $row['keywords'];
echo "<h1><a href='$url'>$title</a></h1>
$keywords<br /><br />";
}
}
?>
</body>
</html>
在此先感謝
先分配i = 0。開始時它沒有價值 – hakiko 2014-12-13 12:45:26
@hakiko它有一個值「$ i ++;」 – BubblewrapBeast 2014-12-13 12:49:23