Loading...


bookmark - Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in

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

 
 Discussion by GaiaZone with 5 Replies.
 Last Update: January 12, 2008, 7:55 pm
 
bookmark - Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in  
Quickly Post to Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in w/o signup Share Info about Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in using Facebook, Twitter etc. email your friend about Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

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!';
}









   Fri Jan 11, 2008    Reply         

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

   Fri Jan 11, 2008    Reply         

It works perfectly.

Thank you. =]

   Fri Jan 11, 2008    Reply         


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

   Sat Jan 12, 2008    Reply         

QUOTE (slu)

...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.
Link: view Post: 367935


That would result in an error if there were no rows matching that query. mysql_num_rows is the best function to use.







   Sat Jan 12, 2008    Reply         

Quickly Post to Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in w/o signup Share Info about Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in using Facebook, Twitter etc. email your friend about Php + Mysql Question! While inserting data into MySQL, how can I know if the data I'm in Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Mysql Php Apache Downloads And Se...

Mysql, PHP, Apache downloads were easy enough. Then came the setup process. This seemed to be an endless mess of going back and forth trying to get the programs to work together properly. Finaly I got them working after about 48 hours. There are great tutorials on the setup process, but only one or ...more

   20-Apr-2008    Reply         

Php And Mysql Programming

hi everyone! I am making a program using php and mysql...I am a noob on this so i need your help guys...I want to make a simple program that will some values and then store them on a database and then retrieve them...uhmm let me give an example out put of what i need. T ...more

   05-May-2008    Reply         

Best Php And Mysql Editor For Noobs

hi there guys, from my previous posting, i am a noob in php and mysql programming. I want to know if there are any php and mysql editors which are best for me as a noob. i appreciate your kindness ...more

   06-May-2008    Reply         

Analog Clock    Analog Clock (4) (4) Counting Variables?   Counting Variables?