Jump to content

Pattern Matching


westminster86

Recommended Posts

How would I write a select statement that fetches rows from a table accordingly. For example, a user enters the letter a into an inputbox, the select statement should retrieve the names of individuals beginning with the letter a.

 

"SELECT * FROM names WHERE name LIKE '%".$searchterm."%'"

 

The above is working, but its not getting back the right values. If a name has the letter a in it, regardless of whether its the first character or not, its being displayed. E.G. Natasha is being displayed and it shouldnt because the user typed in the letter a.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/97229-pattern-matching/
Share on other sites

doesnt work

"Doesn't work" isn't very helpful.

 

Also, to make it case insensitive and prevent SQL injection, you would want something like:

 

<?php
$search = mysql_real_escape_string(strtoupper($searchterm));
$sql = "SELECT * FROM names WHERE UPPER(name) LIKE '{$search}%'";
?>

Link to comment
https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497557
Share on other sites

it runs case-insensitive on my MySQL installation. your mileage may vary.

 

but yes, definitely wrap up any strings you are using in a SQL statement.

 

Good to know, I jump back/forth between MySQL & DB2, and I know I have had case sensitive problems with DB2.

Link to comment
https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497563
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.