Re: Ignoring Case



On or about 6/28/2006 5:57 AM, it came to pass that Mike wrote:
I have set up a simple search in PHP. The user enters some text and
this is then searched for in a MySQL database. If the text is found
within the database, the whole text is displayed, highlighting what the
user entered in red....

Basically...

User enters text in a text box. Result stored as textsearch.

//perform the search in the database
$sql = "select * from mytable where textfield like
'%$_POST[textsearch]%'";

//If found the result of the textfield is stored as $searchres,
changing all occurance of the user entered text to red

$searchres = str_replace("$_POST[textsearch]","<font
color=#FF0000><strong>$_POST[textsearch]</strong><font
color=#000000>",$searchres);

//$searchres is echo'ed

All this works great except if the textfield contains for example
'School' and the user searches on 'school' it won't find it.

I could use various function such as strtolower() etc and try all
possibilities but is there an easier way ignore the case so the search
with pick up both 'School' and 'school'?

Many Thanks

Mike

Take a look at the Mysql LIKE String Comparison Function.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

I'm on a non PHP 5 host and this works for me and is case insensetive and puts rows starting with the search term first
$Like = $_POST['rsargs'][0];
$query = "SELECT Id, Name, City, State, IF (`Name` LIKE '". $Like . "%', '1', '2') AS Order_alias FROM `Venue` WHERE `Name` LIKE '%" . $Like . "%' ORDER BY Order_alias, Name ASC";
$result = mysql_query( $query )
or die('Result is no good: ' . mysql_error());
.



Relevant Pages