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

这我运行此查询在phpMyAdmin的结果是:(正确的)搜索PHPBB的通过MYSQL PHP“TOPIC_TITLE”,但精确匹配不起作用

match this title 
match this title 002 

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

match this title 002 

我也试过对阵同样的结果与PHP和phpMyAdmin的:

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

搜索使用的代码IM的整个街区:

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

任何想法,以什么我做错了吗?

心中已经一直在寻找所有的地方和旁边没有帮助:(发现

回答

1

的问题是,你执行你的查询后,您将获取的第一行,什么都不做吧,通过进入循环取第二行开始打印结果..

如果除去第一$row = mysql_fetch_array($result),(后直接$result = mysql_query($query) or die(mysql_error());)你应该罚款

另一个意见;如果回声一个变量,你不必把任何围绕它的qoutes。而你现在这样做,你将不会得到一个换行符结果,所以你可能想要更改该行echo $topic_title . "<br>";

+0

啊是啊,只是在bash和php之间有点混淆,就像bash一样,你总是需要引号。但是这并不会损害引号的正确性吗?当我看到2个结果作为一个长句时,我还添加了
标签:P(干杯!) – Mint 2010-04-06 22:36:14

+0

没有引号不会伤害。这是一个风格问题。我宁愿将所有变量都放在引号外面。 – 2010-04-07 07:54:55