|
|
|
|
![]() ![]() |
Jan 12 2008, 12:44 AM
Post
#1
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 25 Joined: 8-January 08 From: PR Member No.: 55,961 |
Basically, I want to know if the Data I'm inserting through a Form is already there or not. Sort of a Username registration page.
I have this, but it doesn't appear to work... CODE $result = mysql_query("SELECT * FROM users WHERE username='$username'");
if($result == 1) { echo '<h1>ERROR!</h1>The username you have chosen already exists!'; } |
|
|
|
Jan 12 2008, 12:55 AM
Post
#2
|
|
|
|||[ n00b King ]||| ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 642 Joined: 20-June 07 From: Auckland Member No.: 45,102 |
$result will equal a resource object I beleive so comparing it to 1 will always result in false.
You can use CODE $count = mysql_num_rows($result); Then compare $count == 1 in your if statement. This post has been edited by sonesay: Jan 12 2008, 01:12 AM |
|
|
|
Jan 12 2008, 02:13 AM
Post
#3
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 25 Joined: 8-January 08 From: PR Member No.: 55,961 |
It works perfectly.
Thank you. =] |
|
|
|
Jan 12 2008, 11:12 AM
Post
#4
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 27 Joined: 22-June 05 Member No.: 8,557 |
...or let MySQL do the counting:
CODE $result = mysql_query("SELECT count(*) as count FROM users WHERE username='$username'"); $count = mysql_result($result, 0, "count"); and I would change the compare to $count > 0 in the if statement, just to be sure. |
|
|
|
Jan 13 2008, 03:55 AM
Post
#5
|
|
|
Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software. ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 974 Joined: 25-September 05 From: The dungeon deep below the foundation of trap17 Member No.: 12,251 |
...or let MySQL do the counting: CODE $result = mysql_query("SELECT count(*) as count FROM users WHERE username='$username'"); $count = mysql_result($result, 0, "count"); and I would change the compare to $count > 0 in the if statement, just to be sure. That would result in an error if there were no rows matching that query. mysql_num_rows is the best function to use. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 24th July 2008 - 12:05 PM |