【问题标题】:Error While using MATCH() AGAINST() in php mysql在 php mysql 中使用 MATCH() AGAINST() 时出错
【发布时间】:2016-04-25 06:02:05
【问题描述】:

我是 php 新手。我正在尝试使用 MATCH AGAINST 而不是使用 LIKE 来搜索 mysql dayabase。使用这个脚本,

    <?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 ?

为什么不匹配是有效的? 这个脚本哪里出了问题?

【问题讨论】:

  • 索引是mysql中的保留字

标签: php mysql search


【解决方案1】:

index 在 mysql 中是Reserved Words 必须是反引号

您的查询将是

SELECT `index`, `sura`, `aya`, `text`...

【讨论】:

  • 在更改 SELECT index, sura, aya, text 后出现此错误 Prepare failed: (1191) Can't find FULLTEXT index matching the column list
  • 您的错误请参考link
  • 我已经有了长文本。但是在运行这个 ALTER TABLE bn_bengali ADD FULLTEXT(text);错误仍然存​​在
  • 在您的 phpmyadmin 中使用查询 ALTER TABLE bn_bengali ADD FULLTEXT(text);ALTER TABLE bn_bengali ADD FULLTEXT(sura); 并检查
【解决方案2】:

index 在 mysql 中是Reserved Words 必须是反引号

尝试将您的查询更改为:

SELECT `index`, `sura`, `aya`, `text` FROM...

另外我建议您更改列名,这样您以后也不会遇到问题,因为在进行此更改后可能会出现错误。

【讨论】:

  • 在更改 SELECT index, sura, aya, text 后出现此错误 Prepare failed: (1191) Can't find FULLTEXT index matching the column list
  • @HasanBinKarim,请尝试使用ALTER查询更改您的列名
  • 我把name index改为id但是报错Prepare failed: (1191) Can't find FULLTEXT index matching the column list is still there
  • 你有没有把你的查询也改成@HasanBinKarim,改成SELECT id, sura, aya, text` FROM...`
【解决方案3】:

最好在查询中的每一列都添加“反引号”。因此,即使您使用 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>';
}
    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2011-11-10
    • 2014-04-09
    • 2019-05-17
    • 2013-01-24
    相关资源
    最近更新 更多