Forum Moderators: coopster

Message Too Old, No Replies

Searching a mySQL db with php

Interesting project.

         

GoodMoJo

4:12 am on Oct 15, 2003 (gmt 0)

10+ Year Member



What I'm working on is a client management system. I've only been working with php and mysql for about a week but I've got a good idea how to accomplish everything I need to. But, I've run into a bit of a road block with this CMS project though. Heres my problem:

I have a table full of clients.
I want to be able to search by any field and pull up the record of clients that match the search.

Shrunk down Example Database:

+----------------------+
¦Clients
¦----------------------¦
¦1. name: Miller
¦----------------------¦
¦2. name: McConkey
+----------------------+

So I tried using the LIKE clause in the SQL query but it didn't work how I would have expected it to.

select * from clients where 'name' LIKE '%m%'
Works Just as I would expect and selects both entires Miller and McConkey.
However;
select * from clients where 'name' Like '%mcc%'
My intuition tells me that this should select the McConkey record... MySql actually selects nothing. But on the plus side it doesn't give me an error.

So am I just miss using the LIKE command? I know there must be a way to search a db, I mean what good is a database full of information if you can't search through it?
I know that if I wanted to I could select the entire database and then use the String manipulation commands in php to sort them out but that seems grossly inefficent.

Thanks in advance for any help.
-Chris McConkey

jamesa

7:05 am on Oct 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the single quotes from around the field name, or use backticks.

jatar_k

7:58 am on Oct 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld GoodMoJo,

try jamesa's suggestion and if not let's take another look ;)

bcc1234

8:22 am on Oct 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>select * from clients where 'name' Like '%mcc%'

I don't use mysql, but wouldn't that expression always evaluate to false?

I mean,
'name' like '%XXX%'
would only be true when XXX is a string 'name'

GoodMoJo

1:02 am on Oct 16, 2003 (gmt 0)

10+ Year Member



Thanks jamesa, worked like charm.