Jump to content

Archived

This topic is now archived and is closed to further replies.

-Felix-

Searching MySQL database

Recommended Posts

When I a user registers a new account, he/she must choose a user name.

Of course, I dont want two users to pick the same username, so what I need to know is how to search the MySQL table to find out if a specific user name has already been picked?

 

For example, a user enters desired username: Bob

So I need to search if there already is a user registered with "Bob"

How do I do this?

 

mysql_query("SEARCH.....") ????

 

 

 

 

 

Share this post


Link to post
Share on other sites
if ( select id from table where name = '$name' yields rows ) {
 error
} else {
 continue
}

Share this post


Link to post
Share on other sites

$dbh = mysql_connect('mysql_host:3306','username','password');

mysql_select_db('database');

$query = "SELECT * FROM users_table WHERE username='$username'";

$res = mysql_query($query, $dbh);

$rows = mysql_num_rows($res);

 

if ($rows > 0)

{

echo 'Error!';

}

else

{

// continue to add the user

}

 

mysql_close($dbh);

 

Share this post


Link to post
Share on other sites

why dont you just make username a UNIQUE field? Then just do a normal INSERT and check whether mysql_query is true or false. If false, then the username exists.

Share this post


Link to post
Share on other sites

×

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.