Struggling with simple mysql query

    I have tested a SQL query within phpmyadmin and can get it to work as I need.  basically I have a text field where someone can enter a doctors name i.e. Dr Dawson.... the field this is pulled from is called Doctors, so the query looks like:

SELECT * FROM `maindata` WHERE Doctors LIKE "dr %dawson%"

Open in new window

This works.... so now over to my php query, I have this:

SELECT * FROM maindata WHERE Doctors LIKE 'Dr %".$_GET['field']."%'; 

Open in new window

But this doesn't work.  The initial problem is the Doctors field contains names such as;

Dr James Dawson

So if somesome searches for "Dawson" or "James" it's fine, but if they search for "Dr Dawson" it returns no values due to the James part....

Hope someone can help with the above.

Who is Participating?
Marco GasiFreelancerCommented:
Maybe you could try to use the boolean mode this way:

$field = $_GET['field'];
$words = str_replace(' ', ',', $field);
£sql = "SELECT * FROM maindata WHERE MATCH (Doctors) AGAINST ($words IN BOOLEAN MODE)";

Open in new window

Dave BaldwinFixer of ProblemsCommented:
You have to split it into pieces.  If they are all doctors, I would get rid of the 'Dr' part in the database and add it back when you display it.  Also strip it on input.  That leaves you to search for last name then first name which I would probably put in two separate fields or at least two separate 'LIKE' statements.  I also Never use a $_GET or $_POST directly in a string like that.  I always assign it to a 'regular' variable, limit the size, and trim it so that someone doesn't try to put an entire file in your variable.
"SELECT * FROM maindata WHERE Doctors LIKE '%$lastname%'"; 
"SELECT * FROM maindata WHERE Doctors LIKE '%$lastname%' AND Doctors LIKE '%$firstname%'";

Open in new window

damianb123Author Commented:
This worked perfectly.   And in your very short example you actually taught me how to use that command.  I had seen it, but couldn't get to grips with it.  I'm going to change some other queries now to use that, as it's more powerful, and easier to program.  many thanks for your help.
Marco GasiFreelancerCommented:
Glad to help you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.