Jump to content
xisto Community
Sign in to follow this  
GaiaZone

Php + Mysql Question! While inserting data into MySQL, how can I know if the data Im in

Recommended Posts

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...

$result = mysql_query("SELECT * FROM users WHERE username='$username'");if($result == 1)	{	echo '<h1>ERROR!</h1>The username you have chosen already exists!';	}

Share this post


Link to post
Share on other sites

$result will equal a resource object I beleive so comparing it to 1 will always result in false.You can use

$count = mysql_num_rows($result);

Then compare $count == 1 in your if statement.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

...or let MySQL do the counting:

$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.

Share this post


Link to post
Share on other sites

...or let MySQL do the counting:

$count = mysql_result($result, 0, "count");
linenums:0'>$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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.