arunpatal Posted October 31, 2013 Share Posted October 31, 2013 Hi, i am trying to use charlist wildcard but its not working..... Please check and help I want to display all username starting with S,P and N. <?php require("connect/connection.php"); if (!empty ($_POST['start'])) { $start = $_POST['start']; $result = mysql_query("SELECT * FROM $demo WHERE username LIKE '[$start]%'"); while ($row = mysql_fetch_array($result)) { echo "$row[username] from $row[country]<br/>"; }} ?> <form method='post'> <input type='text' name='start' placeholder="ugn" size='50'/><br><br> <input type='submit' value='Search' /> </form> Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 31, 2013 Share Posted October 31, 2013 I'm guessing that you're after S, P or N... not and! Try using RLIKE instead: http://dev.mysql.com/doc/refman/5.0/en/regexp.html Otherwise you'll need to explode the string and recreate a statement with OR's between... all within the LIKE Quote Link to comment Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author Share Posted October 31, 2013 (edited) I'm guessing that you're after S, P or N... not and! Try using RLIKE instead: http://dev.mysql.com/doc/refman/5.0/en/regexp.html Otherwise you'll need to explode the string and recreate a statement with OR's between... all within the LIKE True.... I want to display all username starting with S, P and N... I am following this tutorial but still not working....... http://www.w3resource.com/sql/wildcards-like-operator/wildcards-charlist.php Tutorial Edited October 31, 2013 by arunpatal Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 31, 2013 Share Posted October 31, 2013 I am following this tutorial but still not working....... http://www.w3resource.com/sql/wildcards-like-operator/wildcards-charlist.php Tutorial That appears to be a feature of SQL and not MySQL. Quote Link to comment Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author Share Posted October 31, 2013 How do i use this with mysql ??? please help me Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 31, 2013 Share Posted October 31, 2013 I'm sure there's a better way, but you could try something like this: <?php $start = str_split($_POST['start']); foreach($start as $currKey=>$currLetter) { $start[$currKey] = "username LIKE '" . $currLetter . "%'"; } $result = mysql_query("SELECT * FROM $demo WHERE " . implode(' OR ', $start)); ?> Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 31, 2013 Share Posted October 31, 2013 Square brackets usually denote an OR list (set), so if you truely want it to start with "SPN" remove the square brackets... Quote Link to comment Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author Share Posted October 31, 2013 So am using it like this now....... but the problem is that it shows all username which have S P N in it. <?php require("connect/connection.php"); $result = mysql_query("SELECT * FROM $demo WHERE username RLIKE '[SPN]'"); while ($row = mysql_fetch_array($result)) { echo "$row[username] from $row[country]<br/>"; } ?> I want only those username which are starting with letter S P and N Quote Link to comment Share on other sites More sharing options...
Solution DavidAM Posted October 31, 2013 Solution Share Posted October 31, 2013 You need to anchor it to the beginning of the string WHERE username RLIKE '^[SPN]' Quote Link to comment Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author Share Posted October 31, 2013 You need to anchor it to the beginning of the string WHERE username RLIKE '^[SPN]' That was it Thanks to all who helped me Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 31, 2013 Share Posted October 31, 2013 FYI there are many cheatsheets available for regulare expressions, here's first one I just found: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ Quote Link to comment Share on other sites More sharing options...
arunpatal Posted October 31, 2013 Author Share Posted October 31, 2013 FYI there are many cheatsheets available for regulare expressions, here's first one I just found: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ Thanks mantalist Quote Link to comment Share on other sites More sharing options...
Barand Posted October 31, 2013 Share Posted October 31, 2013 Without the regexp you could use ...WHERE SUBSTRING(name, 1, 1) IN ('S', 'P', 'N'); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.