Avatar of damianb123
damianb123
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Struggling with simple mysql query

Hi,
    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.

Thanks
MySQL ServerPHPDatabases

Avatar of undefined
Last Comment
Marco Gasi

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Marco Gasi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

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

damianb123

ASKER
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 Gasi

Glad to help you.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23