Jump to content

Recommended Posts

I am very new to this, so I wish to ask a simple question about mysql.
I have built a table already, however, when signup, I wish that the php script could search for any repetitions in username before it lets the person use that username.
Thanks
Ted.
When they submit the membership form, use a query like this:

[code]<?php
$sql = "SELECT unique_id_column FROM member_table WHERE username = '{$_POST['username']}'";
$result = mysql_query($sql);
?>[/code]

Then use the mysql_num_rows() function...

[code]<?php
if (mysql_num_rows($result) > 0){
  echo "Username taken\n";
else {
  // Insert the new user into the database
}
?>[/code]

Regards
Huggie
I edited that code slightly...

mysql_num_rows() returns the number of rows found when the query's executed.  If it found more than 0 rows (> 0) then the username must exist, meaning you can't have that username.

Regards
Huggie
Basically it saves query usage if you insert a name (or you called it a word?)

What unique_id_column is, is just a column name in your table, it's doesn't actually matter what column you're calling for as long as your WHERE clause is correctly taken care of. For example, if my table looks like this;

[table]
[tr][td]id[/td][td]username[/td][td]password[/td][/tr]
[tr][td]1[/td][td]kamasheto[/td][td]password[/td][/tr]
[tr][td]2[/td][td]username[/td][td]password[/td][/tr]
[/table]

And I try registering the username kamasheto with this query;

[code]SELECT id FROM members WHERE username='kamasheto';[/code]
Then I'll get this row returning;

[table]
[tr][td]id[/td][/tr]
[tr][td]1[/td][/tr]
[/table]

This when passed through mysql_num_rows(); will give me a true value of 1, which will produce an error as desired. If you decide you want to select * (means all the columns), your query;

[code]SELECT * FROM members WHERE username='kamasheto';[/code]

will return

[table]
[tr][td]id[/td][td]username[/td][td]password[/td][/tr]
[tr][td]1[/td][td]kamasheto[/td][td]password[/td][/tr]
[/table]

Which when passed through mysql_num_rows() will give the same result, 1 returned row. The only limitation to calling a specific column(s) is that in your fetching process (using mysql_fetch_array with the while loop), you'll be able to only use those columns. For example in my first query where I called for ONLY the id, I cannot check for the password using that same query, I'll have to call for a new query hence. However with my second query, I can always fetch the entire array anytime down the script.

Helpful enough?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.