Author Topic: Searching MySQL database  (Read 1672 times)

0 Members and 1 Guest are viewing this topic.

Offline -Felix-Topic starter

  • Enthusiast
  • Posts: 119
    • View Profile
Searching MySQL database
« on: October 08, 2005, 09:31:47 AM »
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.....") ????





Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Searching MySQL database
« Reply #1 on: October 09, 2005, 12:24:02 AM »
Code: [Select]
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/

Offline mysqlautobackup

  • Irregular
  • Posts: 1
    • View Profile
Searching MySQL database
« Reply #2 on: October 10, 2005, 08:52:26 AM »
$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

Offline ryanlwh

  • Staff Alumni
  • Addict
  • *
  • Posts: 1,924
    • View Profile
Searching MySQL database
« Reply #3 on: October 13, 2005, 04:20:56 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
Code: [Select]
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...