"Andy Levy" <an*******@hotmail.com> wrote in message
news:O_**************@news-binary.blueyonder.co.uk...
Hi
Im trying to create a function that tests if a 'Username' exists in a
particular table. If the value is found then the code generates
'Username1' as the username, and check if that username exists. If it does, it
continues onto generating 'Username2' and so on.
<?
//testing with username andrew
$name = "andrew";
mysql_pconnect("server", "username", "pass");
mysql_select_db("db");
$result = mysql_query("SELECT Username FROM users WHERE Username =
'$name'");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0):
$count = 1;
While ($num_rows > 0):
//$username = $username.$count;
$result = mysql_query("SELECT Username FROM users WHERE Username =
'$name'");
$num_rows = mysql_num_rows($result);
print($username.$count);
$count = $count + 1;
endwhile;
//----
else:
print("ok");
endif;
?>
Your above method is very performance ineffective - You have the potential
of having alot of read/write to the disk when it is unnesscessary... I'm new
to MySQL... and believe you could use "LIKE" in your query - I've known it
exists and I just played around with it for the first time... but you could
try something like
$result=mysql_query("SELECT Username FROM users WHERE Username LIKE
'$name%';
I believe the percent sign acts as a wild card, so it should catch
"username", "username1", "username999" all in one go... Play around with it
and look up the 'LIKE' condition for more info... It is bound to be far more
environmentally friendly on your systems resources than your existing
method... You could go one step further and perform an ORDER BY Username and
LIMIT too... this would help you determine the next username that you want
to use...
Hope that helps...
randelld