473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Testing if a value exists.

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
3 17318
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence...
2
by: PeterW | last post by:
I have a data entry form based on a query. As a user enters data in the fields, I want to check if that data exists in the underlying table. I have tried Dlookup on the table and also a...
4
by: Michael McDowell | last post by:
Hi How might I test for property being <undefined> before I try and use it Ta in advance Michael McD.
6
by: Lance | last post by:
Hi, How do you test for an object existing in C#? C# seems to differentiate between 'null' and non-existance. In other languages I could test as so: if(ex.InnerException.StackTrace != null)...
2
by: MS Newsgroups | last post by:
Hi, I am trying to build a function that returns a dataset.When populating the dataset, is it possible to test if a value already exists in a column and only add it if it is not already present...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
6
by: Strato | last post by:
Hi folks, I want to write some kind of test to check at startup if another instance of my script is already running. I don't want to handle writing of a PID file because it is too Unix/Linux...
6
by: JRough | last post by:
How do I test for no data in a string? I tried if !(isset($data)) and $data =="" and neither one returns the message when there are no records found. Or is it a better idea to do the test at the...
11
by: chemlight | last post by:
I'm having a problem. I'm sure I'm going to kick myself over the answer... I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.