By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Testing if a value exists.

P: n/a
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;
?>
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"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
Jul 17 '05 #2

P: n/a
I noticed that Message-ID:
<O_**************@news-binary.blueyonder.co.uk> from Andy Levy contained
the following:
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.


try this:

$db = mysql_connect("host", "user", "pass");
mysql_select_db("dbname",$db);
$name = "michael";
$result = mysql_query("SELECT Username FROM users WHERE
Username='$name'",$db);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0):
$count = 1;
While ($num_rows > 0):
$newname = $name.$count;
$result = mysql_query("SELECT Username FROM users WHERE
Username='$newname'",$db);
$num_rows = mysql_num_rows($result);
print($newname);
$count = $count + 1;
endwhile;

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3

P: n/a
Thanks Geoff & Randell

That is very useful. I am going to use your help. I have also realised
that i do not need an iterating loop though. If i just add the (numRows +1)
onto the end of the username i should never come across a duplicate
username. If the SQL uses the 'Like' operator then it should pull up all
rows.
"Geoff Berrow" <bl******@ckdog.co.uk> wrote in message
news:jt********************************@4ax.com...
I noticed that Message-ID:
<O_**************@news-binary.blueyonder.co.uk> from Andy Levy contained
the following:
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.


try this:

$db = mysql_connect("host", "user", "pass");
mysql_select_db("dbname",$db);
$name = "michael";
$result = mysql_query("SELECT Username FROM users WHERE
Username='$name'",$db);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0):
$count = 1;
While ($num_rows > 0):
$newname = $name.$count;
$result = mysql_query("SELECT Username FROM users WHERE
Username='$newname'",$db);
$num_rows = mysql_num_rows($result);
print($newname);
$count = $count + 1;
endwhile;

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.