MySQL Search

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

MySQL Search

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: MySQL Search

Post 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.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: MySQL Search

Post by the9ulaire »

Thanks. I've got it working now!
Post Reply