Jump to content


Photo

Searching MySQL database


  • Please log in to reply
3 replies to this topic

#1 -Felix-

-Felix-
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 08 October 2005 - 01:31 PM

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.....") ????






#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 09 October 2005 - 04:24 AM

if ( select id from table where name = '$name' yields rows ) {
  error
} else {
  continue
}

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 mysqlautobackup

mysqlautobackup
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 10 October 2005 - 12:52 PM

$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);


MySQL Auto Backup

Professional MySQL Backup Automation Software on Windows

http://www.mysqlautobackup.com http://www.swordsky.com

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 13 October 2005 - 08:20 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users