Php search function
I am trying to create a search function for user profiles on my site.
$search= $_POST['search'];
$res=mysql_query("SELECT * FROM ".TBL_USERS." WHERE username LIKE '$search%'");
This is the code I am using. This will only work if you find something that matches the beginning of the result. Is there a way to return values that are of what I am typing as a non-uppercase or lowercase part of the username?
Thankyou
a source to share
"%" is a wildcard, so if you also place it in front of your search string (for example %$search%
), it will match $ search anywhere in the username.
Use "LOWER" in SQL to make your username lowercase and "strtolower" in PHP to do the same, then run your query to get case insensitive results.
And as David Doorvar said: read bobby-tables.com before you do anything else!
a source to share
When you use LIKE, you can use the% wildcard on both sides of the input, this will return all users where $ search is part of the username.
You can also have a look at MySQL REGEXP function
$search= mysql_real_escape_string($_POST['search']);
$res=mysql_query("SELECT * FROM ".TBL_USERS." WHERE username LIKE '%" . $search . "%'");
a source to share