我是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?
为什么不匹配正在发挥作用? 这个脚本中的问题在哪里?
答案 0 :(得分:3)
答案 1 :(得分:1)
index
在mysql中是Reserved Words,它必须在反引号中
尝试将您的查询更改为:
SELECT `index`, `sura`, `aya`, `text` FROM...
此外,我建议您更改列名,以便将来也不会遇到问题,因为在执行此更改后可能会出现错误。
答案 2 :(得分:1)
最好在查询中为每一列添加“反引号”。所以,如果你使用甚至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>';
}
?>