1
$sql = sprintf( "SELECT topic_title 
                   FROM `phpbb_topics` 
                  WHERE `topic_title` LIKE '%%%s%%' LIMIT 20"
               , mysql_real_escape_string('match this title')
              );

我在 phpMyAdmin 中运行此查询的结果是:(正确)

match this title
match this title 002

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

match this title 002

我也尝试过 MATCH AGAINST,结果与 php 和 phpMyAdmin 相同:

$sql = "SELECT topic_title 
          FROM phpbb_topics 
         WHERE MATCH (topic_title) 
               AGAINST('match this title' IN BOOLEAN MODE)";

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

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";
}

关于我做错了什么的任何想法?

我一直在到处寻找,几乎没有发现任何帮助:(

4

1 回答 1

1

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

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

另一个评论;如果你回显一个变量,你不必在它周围放置任何 qoutes。并且以您现在的方式,您不会在结果之间获得换行符,因此您可能希望将该行更改为echo $topic_title . "<br>";

于 2010-04-06T13:34:34.713 回答