5

i need to query my database and find results :

mysql_query("select * from ".ALU_TABLE." where username like '%$q%' or name like '%$q%'");

if i have a name in my table such as Book and i enter book in search box it wont show the Book

i need to query my database as not to be case sensitive.

Mac Taylor
  • 5,020
  • 14
  • 50
  • 73
  • possible duplicate of [case insensitive for sql LIKE wildcard statement](http://stackoverflow.com/questions/2876789/case-insensitive-for-sql-like-wildcard-statement) – OMG Ponies Jun 12 '10 at 17:07
  • Also: http://stackoverflow.com/search?q=mysql+case+insensitive – OMG Ponies Jun 12 '10 at 17:07

2 Answers2

4

You can use the LOWER() function

... WHERE LOWER(username) LIKE blabl OR LOWER(name) LIKE asdasd
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
  • 2
    This works. But you will not allow the db to use indexes if you mention the column names as parameters to LOWER(), so it might be quite slow. – O. Jones Jun 12 '10 at 14:12
3

You need to append something like COLLATE utf8_general_ci to your query. (The _ci suffix stands for case insensitive.)

Have a look at the documentation: 9.1.7.1. Using COLLATE in SQL Statements:

With the COLLATE clause, you can override whatever the default collation is for a comparison. COLLATE may be used in various parts of SQL statements.

aioobe
  • 413,195
  • 112
  • 811
  • 826