Forum Moderators: coopster

Message Too Old, No Replies

Selecting from database (regardless of case)

Selecting from database (regardless of case)

         

PumpkinHead

8:25 am on Sep 26, 2005 (gmt 0)

10+ Year Member



Hi,

I have a phonebook function where the user can enter a first name/surname etc. The form then accesses a database (through ODBC..not MySQL) that selects any records with matching details.

The problem I have is that most users enter details in lower case (I.e myname), and the entries on the database can be any case at all (I.e Myname or MyName).

Whats the best solution to this problem? I was thinking about changing both the input name and database name to upper case before comparing? If so, is there a php function to do this?

Many thanks :)

grandpa

10:40 am on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PHP has three functions to handle case in strings.

$string = strtolower($string); // make it lower case
$string = strtoupper($string); // make it UPPER case
$string = ucwords($string); // make it Upper Case First Letter

I believe that you can use a wildcard in your query and it will ignore case.

$sql = "SELECT * FROM table WHERE field LIKE 'C%' ";
I'm pretty sure this will return everything that begins with the letter C, irrespective of case.

chrisjoha

10:51 am on Sep 26, 2005 (gmt 0)

10+ Year Member



You can also lower case values with MySQL:
"SELECT * FROM TABLE WHERE lower(field) = lower($value)"

PumpkinHead

11:14 am on Sep 26, 2005 (gmt 0)

10+ Year Member



Many thanks both, the code chrisjoha provided was exactly what I was looking for.

tomda

12:27 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP has three functions to handle case in strings.

And...
$string = ucfirst($string); // make it Upper case first character

grandpa

3:27 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I started to say
PHP has at least three functions to handle case in strings

Then changed my mind...