Problem with MySQL Search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

Problem with MySQL Search

Post by slaterino »

Hi,
I have created what I think is quite a simple search function but at the moment it keeps throwing back 'No results found' at me and I can't work out why. The script is quite small so I've included it here. Can anyone see what the problem might be? I've double-checked everything many times in case there's a mistake but can't see anything.

This is the form:

Code: Select all

<form method="POST" action="search.php"> Search the code repository:<br /> <input type="text" id="keyword" name="keyword" /><br /> <input type="submit" value="Search!" /> </form>


And this is the search.php file:

Code: Select all

<?php
 
mysql_connect("128.0.0.1","asad","secret");
 
mysql_select_db("asad");
 
  $keyword = mysql_real_escape_string($_POST['keyword']);
 
  // Perform the fulltext search
  $query = "SELECT ID, FirstName, LastName, Organisation, OrgType 
            FROM Contacts WHERE MATCH(OrgType) AGAINST ('$keyword')";
 
  $result = mysql_query($query);
 
  // If results were found, output them
  if (mysql_num_rows($result) > 0) {
 
    printf("Results: <br />");
 
    while ($row = mysql_fetch_array($result)) {
 
      printf($row['FirstName'], $row['LastName'], $row['Organisation']);
 
    }
 
  } else {
    printf("No results found");
  }
 
?>
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Problem with MySQL Search

Post by marcth »

I probably shouldn't be responding to your post as I haven't used mySQL for several years. Is case-sensitivity the problem?
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: Problem with MySQL Search

Post by Cut »

I think it's matching against "$keyword," literally. You could try echoing $query to check. If it is, use:

Code: Select all

$query = "SELECT ID, FirstName, LastName, Organisation, OrgType FROM Contacts WHERE MATCH(OrgType) AGAINST ('".$keyword."')";
Post Reply