views:

24

answers:

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

Which I run this query in phpMyAdmin the results are: (correct)

match this title
match this title 002

But when I run that same MYSQL query in PHP I get: (incorrect)

match this title 002

I have also tried MATCH AGAINST with the same result with both php and phpMyAdmin:

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

The whole block of code im using to search with:

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

Any idea as to what i'm doing wrong?

I'v been searching all over the place and have found next to no help :(

+1  A: 

The problem is that after you execute your query you fetch the first row, do nothing with it, enter the loop by fetching the second row and start printing results..

If you remove the first $row = mysql_fetch_array($result), (directly after $result = mysql_query($query) or die(mysql_error());) you should be fine.

Another comment; If you echo a variable you don't have to put any qoutes around it. And in the way you're doing it now, you won't get a newline between the results so you might want to change that line to echo $topic_title . "<br>";

Lex
Ah yeah, just getting a bit confused between bash and php, as with bash you pretty much always need the quotes. But it doesn't hurt having the quotes right? I also added the <br /> tags after I saw that 2 results came in as one long sentence :P (Cheers!)
Mint
No the quotes won't hurt. It's a matter of style. I prefer to put all my variables out of the quotes.
Lex