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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.