westminster86 Posted March 21, 2008 Share Posted March 21, 2008 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 More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 remove the first percent sign in your query. that will get only those records starting with the search term. Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497509 Share on other sites More sharing options...
westminster86 Posted March 21, 2008 Author Share Posted March 21, 2008 doesnt work Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497542 Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 the SQL for finding records where the column name starts with 'a' is: SELECT * FROM names WHERE name LIKE 'a%' that doesn't work for you? are you getting any errors? nothing? the SQL runs as described here. Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497551 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 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 More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 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. Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497559 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 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 More sharing options...
westminster86 Posted March 21, 2008 Author Share Posted March 21, 2008 bluesky, i dnt actually know what the user is entering i was just using A as an example, if i dnt know wht the first charcter is whta do i put? an underscore? Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497587 Share on other sites More sharing options...
rhodesa Posted March 21, 2008 Share Posted March 21, 2008 replace "SELECT * FROM names WHERE name LIKE '%".$searchterm."%'" with "SELECT * FROM names WHERE name LIKE '".mysql_real_escape_string($searchterm)."%'" if that isn't working, then you will need to post more of your code Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497597 Share on other sites More sharing options...
westminster86 Posted March 21, 2008 Author Share Posted March 21, 2008 ok got it to work thanks for your help Link to comment https://forums.phpfreaks.com/topic/97229-pattern-matching/#findComment-497608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.