English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to create a registration system with PHP and MySQL. At the moment, I am trying to set it up so that, if a user tries to register with a name that's already in the database, they get a "Sorry, that name's already taken-- try again" message. Otherwise, their information gets added to the database.

The database name is bookmarks. The name of the table is user, and name of the field is username.

How do I query the database to determine if the username already exists?

2006-10-26 07:44:59 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

//assume that the text field name is txtUsername
$username = $_POST['txtUsername'];
//connect to the server using your hostname, username, and password
mysql_connect($host,$usr,$pwd) or die (mysql_error());
//select the database
mysql_select_db('bookmarks') or die (mysql_error());
//get a result set of the query
$rs = mysql_query("select * from user where username = '$username'");
if(mysql_num_rows($rs) != 0){ // there is a user with that username aready
//echo the error message here
}

2006-10-26 07:54:00 · answer #1 · answered by Phoenixheart 2 · 1 0

to be efficient and not waste gobs of memory, never query for * on a table, always use column names. Thus your query to check for a user name should be "SELECT userName FROM bookmarks.user WHERE username='$userName'"

If that returns a record, that user name is in use.

2006-10-26 08:46:18 · answer #2 · answered by John J 6 · 0 0

There is a pretty good article on this at http://www.phpfreaks.com/tutorials/65/0.php
I will not try to paste the code as Yahoo will just chop it up.

2006-10-26 07:56:14 · answer #3 · answered by Interested Dude 7 · 0 0

fedest.com, questions and answers