Forum Moderators: coopster

Message Too Old, No Replies

[NEWB] PHP Search Operators?

Trying to figure out how to use "begins with", "contains","is equal to" etc

         

Beandip

8:07 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



I know this is going to be a really dumb question but I can not for the life of me figure out the proper terminology in order to search these forums for the answer I am sure someone has already asked. So I apologize.

When constructing a simple search form, with perhaps a single field called "Last Name." I want the user to be able to enter perhaps just the first letter of the last name and have the search results return all matching records. For example: Last Name = c

Returns...

Collins
Compton
Copperton

Obviously the following code returns data that "Is equal to" the data. Which would be a lastname that is the letter C.

$sql = "select * from Tablename where Lastname=\"C\"";

In PHP how do you code it for things like...

begins with
contains
is equal to
is not equal to

etc.

If you know if a good primer or another forum already discussing this, I'd love a link to it. Like I said I keep trying to search for keywords that I think describe this and I come up with nothing.

Thanks!
Scott

coopster

8:17 pm on Feb 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Beandip.

Although PHP has the features you are referring to, you really are asking an SQL question, not a PHP question. The keyword you are looking for in your SQL database is the LIKE string operator.

interactive gun

8:24 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



$sql = "select * from Tablename where Lastname LIKE '%$c%' if your looking for any name with a c in it. like foccer.

or LIKE 'c%' if the name starts with c. like chris.

! is equal to no. so,

"select * from Tablename where Lastname!= 'Chris' would return all entries except chris.

why not look at the php code generated by phpMyAdmin, or the mysql manual itself?
[dev.mysql.com...] (for english version)

you don't need \"\" in a query.

Beandip

11:24 pm on Feb 28, 2005 (gmt 0)

10+ Year Member



Thanks a lot. These suggestions/hints got me pointed in the right direction. I look forward to utilizing this great resource here.