woocha Posted August 10, 2008 Share Posted August 10, 2008 Hi Guys.. I need to read all of the rows from a MySQL table and return only the results that start with a user input letter. So like if the user types in Andrew. I want the database to return all rows that have usernames beginning with the letter a. Has anyone ever done anything like this before? Thanks Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/ Share on other sites More sharing options...
webref.eu Posted August 10, 2008 Share Posted August 10, 2008 So you just need to: - Get first letter of user input. - Write a query that selects all Usernames beginning with that letter. Correct? Rgds Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/#findComment-612942 Share on other sites More sharing options...
webref.eu Posted August 10, 2008 Share Posted August 10, 2008 echo $variable[0]; will echo the first character of a variable, so you can use something similar in your code to retrieve the first letter of user input. Rgds Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/#findComment-612943 Share on other sites More sharing options...
woocha Posted August 10, 2008 Author Share Posted August 10, 2008 Thanks guys, but I am more worried about the SQL query...I mean If I use substr to finms the first letter of the name, how to I write the SQL to search all usernames that begin with that substring? Is there an SQL function like $query = "SELECT username FROM users WHERE username starts with a"; ? I know it looks stupid, but is there anything like that? Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/#findComment-612955 Share on other sites More sharing options...
cooldude832 Posted August 10, 2008 Share Posted August 10, 2008 try <?php $user_input = trim($_POST['name']); $user_input = mysql_real_escape_string(substr($user_input,0,1)); $q = "SELECT GROUP_CONCAT(Username) as usernames FROM `users` Where username LIKE '".$user_input."%'"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); print_r(mysql_fetch_assoc($r)); ?> Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/#findComment-612960 Share on other sites More sharing options...
webref.eu Posted August 10, 2008 Share Posted August 10, 2008 $FirstLetter = $variable[0]; I can't give you the exact SQL but a guess is: $query = "SELECT username FROM users WHERE username LIKE '" . $FirstLetter . "%'"; the above is probably rather wrong syntax-wise but you get the idea (please feel free to correct anyone). Just use a LIKE statement, then the FirstLetter followed by a wildcard match. Rgds Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/#findComment-612964 Share on other sites More sharing options...
woocha Posted August 10, 2008 Author Share Posted August 10, 2008 LIKE and Wildcard % awesome...that is just what I was looking for guys. Thanks Link to comment https://forums.phpfreaks.com/topic/119034-read-all-mysql-rows-username-and-return-all/#findComment-612978 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.