[英]Error While using MATCH() AGAINST() in php mysql
我是php的新手。 我試圖使用MATCH AGAINST搜索mysql dayabase而不是使用LIKE。 使用這個腳本,
<?php
if (isset($_GET['q'])){
error_reporting(-1);
$query = $_GET['q'];
$dbh = new mysqli($host, $user, $password, $database);
if ($dbh->connect_error) {
echo 'Unable to connect to database '. $dbh->connect_error;
} else {
if ($stmt = $dbh->prepare("SELECT index, sura, aya, text FROM bn_bengali WHERE MATCH(sura,text) AGAINST(?) "))
{
$stmt->bind_param("s", $query);
$stmt->execute();
$stmt->bind_result($index, $sura, $aya, $text);
$stmt->store_result();
printf("Number of rows: %d.\n", $stmt->num_rows);
while ($stmt->fetch()) {
echo $sura.'-'.$aya;
echo $text;
echo '<hr />';
}
} else {
echo "Prepare failed: (" . $dbh->errno . ") " . $dbh->error;
}
}
} // end isset get q
else
{
echo '<form action="" ><input type="text" name="q"><button>Search</button></form>';
}
?>
但它給出了這個錯誤,
Prepare failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index, sura, aya, text FROM bn_bengali WHERE MATCH(sura,text) AGAINST(?)' at line 1
這個腳本中的問題在哪里?
我想搜索匹配的數據庫表。
但是同樣的腳本工作正常
SELECT sura,aya,text FROM bn_bengali WHERE text LIKE?
為什么不匹配正在發揮作用? 這個腳本中的問題在哪里?
index
是mysql中保留的字必須是反引號
嘗試將您的查詢更改為:
SELECT `index`, `sura`, `aya`, `text` FROM...
此外,我建議您更改列名稱,以便將來也不會遇到問題,因為執行此更改后可能會出現錯誤。
最好在查詢中的每一列添加“反引號”。 所以,如果你使用甚至mysql保留關鍵字,那么它不會產生任何問題。 試試下面的代碼。
if ($dbh->connect_error) {
echo 'Unable to connect to database '. $dbh->connect_error;
} else {
if ($stmt = $dbh->prepare("SELECT `index`, `sura`, `aya`, `text` FROM bn_bengali WHERE MATCH(sura,text) AGAINST(?) "))
{
$stmt->bind_param("s", $query);
$stmt->execute();
$stmt->bind_result($index, $sura, $aya, $text);
$stmt->store_result();
printf("Number of rows: %d.\n", $stmt->num_rows);
while ($stmt->fetch()) {
echo $sura.'-'.$aya;
echo $text;
echo '<hr />';
}
} else {
echo "Prepare failed: (" . $dbh->errno . ") " . $dbh->error;
}
}
} // end isset get q
else
{
echo '<form action="" ><input type="text" name="q"><button>Search</button></form>';
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.