在我的腳本中,如果行與用戶輸入的行相似,則用戶輸入表單並從MYSQL表中返回行。我正在建立一個搜索引擎,一切都基於等級。但我希望能夠調整下面的代碼,以查看「iPad」這個單詞有多少次與行字段相關,即「標題」,「描述」,「關鍵字」和「鏈接」。如果是這樣,我希望該行返回的高於說具有更高ID的行,但只在所有字段中提及iPad一次。 我的代碼如下: 條款一起查詢: $查詢=「選擇*從掃描WHERE」;如何在MySQL中掃描我的所有字段以進行表單輸入
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " AND ";
}
$query .= "title LIKE '%{$each}%' OR link LIKE '%{$each}%' OR keywords LIKE '%{$each}%' OR description LIKE '%{$each}%' ";
}
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error($connect));
echo '<p class="time">Qlick showed your results in ' . number_format($secs,2) . ' seconds.</p>';
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$rank = $row['rank'];
Seperate Terms Query
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0){
$query .= " OR ";
}
$query .= "title LIKE '%{$each}%' OR link LIKE '%{$each}%' OR keywords LIKE '%{$each}%' OR description LIKE '%{$each}%' ";
}
// Don't append the ORDER BY until after the loop
$query = mysql_query($query) or die('MySQL Query Error: ' . mysql_error($connect));
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$rank = $row['rank'];
你嵌套的MySQL查詢,這是不是一個好主意。它可能適用於一個小數據集,但是一旦你經過了幾百行,它就結束了。 – Josh