Link to home
Start Free TrialLog in
Avatar of damianb123
damianb123Flag 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
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of 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.
Glad to help you.