首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过MYSQL php搜索phpbb的'topic_title‘,但精确匹配不起作用

问通过MYSQL php搜索phpbb的'topic_title‘,但精确匹配不起作用
EN

Stack Overflow用户
提问于 2010-04-06 12:55:51
回答 1查看 180关注 0票数 1
代码语言:javascript
复制
$sql = sprintf( "SELECT topic_title 
                   FROM `phpbb_topics` 
                  WHERE `topic_title` LIKE '%%%s%%' LIMIT 20"
               , mysql_real_escape_string('match this title')
              );

我在phpMyAdmin中运行这个查询,结果是:(正确)

代码语言:javascript
复制
match this title
match this title 002

但是当我在PHP中运行相同的MYSQL查询时,我得到:(不正确)

代码语言:javascript
复制
match this title 002

我也尝试过用php和phpMyAdmin进行匹配,结果是一样的:

代码语言:javascript
复制
$sql = "SELECT topic_title 
          FROM phpbb_topics 
         WHERE MATCH (topic_title) 
               AGAINST('match this title' IN BOOLEAN MODE)";

我用来搜索的整个代码块如下:

代码语言:javascript
复制
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("phpbb") or die(mysql_error());         
$query = "match this title";
$query = "SELECT topic_title 
    FROM phpbb_topics 
    WHERE MATCH (topic_title) 
          AGAINST('$query' IN BOOLEAN MODE)"; 
// Doesn't work (these 2 both give the same result "match this title 002" and no the "match this title")
// $query = "SELECT * FROM `phpbb_topics` 
//    WHERE `topic_title` 
//    LIKE '%$query%' 
//    LIMIT 0, 30 "; // Doesn't work
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    $topic_title = $row['topic_title'];
    echo "$topic_title";
}

你知道我做错了什么吗?

我到处找,几乎找不到任何帮助:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-04-06 21:34:35

问题是,在执行查询之后,获取第一行,不对其执行任何操作,通过获取第二行进入循环,并开始打印结果。

如果删除第一个$row = mysql_fetch_array($result) (紧跟在$result = mysql_query($query) or die(mysql_error());之后),应该没问题。

另一个注释;如果你回显一个变量,你不需要在它周围加上任何qoutes。按照现在的方式,您不会在结果之间换行,所以您可能希望将该行更改为echo $topic_title . "<br>";

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2582705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档