Page 1 of 1

MySQL Search

Posted: Wed Feb 13, 2008 6:39 pm
by the9ulaire
I am trying to build a simple PHP search to query my MySQL database. However, it only works part of the time. I am not really familiar with how to build a search. I built my code from the ideas of a book I bought a while back. Here is my code:

Code: Select all

 
$sql = "SELECT *, MATCH (name, full_desc, short_desc, address) AGAINST ('" . $_GET['keywords'] . "') AS score " .
        "FROM activity WHERE MATCH (name, full_desc, short_desc, address) AGAINST ('" . $_GET['keywords'] . "') " .
        "ORDER BY score DESC";
 
$result = mysql_query($sql, $conn) or die("Could not preform search: " . mysql_error());
 
My search will work on some, while it won't work on others. My index covers the four fields: name, full_desc, short_desc, address. What am I doing wrong here? What can I do to make my search work properly and better overall?

Thanks guys!
lwr

Re: MySQL Search

Posted: Fri Feb 15, 2008 12:04 am
by Benjamin
I looked at your code and saw that you are not preparing the variables being placed into your query for MySQL. You mentioning that it only works part of the time, so I am pretty confident that your query is failing when certain characters such as ' are being entered. The solution is to use mysql_real_escape_string on variables before executing database queries containing them.

Re: MySQL Search

Posted: Fri Feb 15, 2008 12:49 pm
by the9ulaire
Thanks. I've got it working now!