473,800 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("SE LECT Username FROM users WHERE Username =
'$name'");
$num_rows = mysql_num_rows( $result);

if ($num_rows > 0):
$count = 1;
While ($num_rows > 0):
//$username = $username.$coun t;
$result = mysql_query("SE LECT 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 17361
"Andy Levy" <an*******@hotm ail.com> wrote in message
news:O_******** ******@news-binary.blueyond er.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("SE LECT Username FROM users WHERE Username =
'$name'");
$num_rows = mysql_num_rows( $result);

if ($num_rows > 0):
$count = 1;
While ($num_rows > 0):
//$username = $username.$coun t;
$result = mysql_query("SE LECT 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_q uery("SELECT Username FROM users WHERE Username LIKE
'$name%';

I believe the percent sign acts as a wild card, so it should catch
"username", "username1" , "username99 9" 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.blueyond er.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",$d b);
$name = "michael";
$result = mysql_query("SE LECT 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("SE LECT Username FROM users WHERE
Username='$newn ame'",$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.c om...
I noticed that Message-ID:
<O_************ **@news-binary.blueyond er.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",$d b);
$name = "michael";
$result = mysql_query("SE LECT 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("SE LECT Username FROM users WHERE
Username='$newn ame'",$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
21225
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 an integer) into an array. To prevent rechecking already processed cells, the script iterates through the (sorted) array to see whether that integer is an element of the array. After reading about javascript arrays a bit more, I thought...
2
1993
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 recordset.open with a select query the error message I get trying to open the recordset is along the lines of :
4
1957
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
1959
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) nvc.Add("** INNER EXCEPTION", ex.InnerException.StackTrace.ToString()); But an exception gets thrown at the if(...) statement. 'Object Reference
2
5611
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 ? I tried adding setting mycolumn.unique=true, but this generates an exception when the first duplicate occur. Any suggestions would be appreciated
72
5286
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: http://geosoft.no/development/unittesting.html Thanks.
6
6392
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 specific way to do this, and I need to keep the code to be cross-platform. I think the better way to achieve this is to use some process control,
6
1330
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 query level and not let the user get a worksheet with no records? The thing is a user could click the button for excel output even if there are no records on the web page, so I would think in that case they would expect a blank worksheet...
11
4758
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 vendor has been added to the table yet. The problem is, if they haven't been added, I can't seem to get the script to realize that. here is what I am trying to do. $testvend = SELECT language FROM vendor_details WHERE id = $vendorid ...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.