note 79082 added to ref.mysql

From: Date: Sat, 10 Nov 2007 04:13:41 +0000
Subject: note 79082 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-132155@lists.php.net to get a copy of this message
I am wanting to increment usernames which are to be alphabetic characters A-Z or a-z and if someone uses "abc" it will automatically be given "abc1" and the next applicant to apply for that same username will automatically be given "abc2", the next "abc3" and so on. If I enter the following into MySQL; SELECT username FROM users WHERE username REGEXP 'abc[0-9*]'; the result is abc1 abc2 abc3 I have been given the following script from another forum but it does not work; <?php $username = 'abc'; $query = "SELECT username FROM users WHERE username LIKE '$username%'"; $res = mysql_query($query); while($row = mysql_fetch_assoc($res)) { $n = str_replace($username, '', $row['username']); if ($old_n+1!=$n) { $new_user = $username . ($old_n+1); break; } } ?> In addition to the script not working the "LIKE" function would not be suitable because if a username was applied as "blue" it would pick up usernames "blue1" and "bluebird1" therefore it appears as if "REGEXP '$username[0-9*]' is the only or correct option. I have tried the REGEXP option in association with the balance of the code but it appears as if PHP will not interact with the "[" and "]" in the following; $query = "SELECT username FROM users WHERE username REGEXP '$username[0-9*]'"; ---- Server IP: 203.89.181.186 Probable Submitter: 202.150.120.81 ---- Manual Page -- http://www.php.net/manual/en/ref.mysql.php Edit -- https://master.php.net/note/edit/79082 Del: integrated -- https://master.php.net/note/delete/79082/integrated Del: useless -- https://master.php.net/note/delete/79082/useless Del: bad code -- https://master.php.net/note/delete/79082/bad+code Del: spam -- https://master.php.net/note/delete/79082/spam Del: non-english -- https://master.php.net/note/delete/79082/non-english Del: in docs -- https://master.php.net/note/delete/79082/in+docs Del: other reasons-- https://master.php.net/note/delete/79082 Reject -- https://master.php.net/note/reject/79082 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132155) next »