PHPFAN10 Posted January 20, 2011 Share Posted January 20, 2011 Hi, Posted here as it is in regards to PHP code making MySQL queries. Let's say i had this code below: <?php # Databse query $query = mysql_query("SELECT id, admin, username, first_name, last_name, email FROM `users` WHERE username = '$username' AND password = '$password' LIMIT 1"); # Check login query if(!$query){ echo 'Oops'; } else { echo 'Login Succesful'; } ?> Now this code with slight difference: <?php # Databse query $query = mysql_query("SELECT id, admin, username, first_name, last_name, email FROM `users` WHERE username = '$username' AND password = '$password' LIMIT 1"); # Check login query if(!$query){ echo 'Oops'; } else { if (mysql_num_rows($query) == 1) { echo 'Login Succesful'; } else { echo 'Login Details Invalid'; } } ?> Now my question is in the second lot of code; i am doing a check to ensure that the query is true in terms of username and password match database login credentials by doing mysql_num_rows as in code . Basically do i need to do that extra check in the second lot of code or is the first lot of code fine in checking username and password match database and if not return oops and return Oops if query fails for any other reason ? not sure if i am creating more code that is needed. I am slightly confused see in whether the extra check in second lot of code is needed or not. Could someone please explain in simple terms to me if it is or not and why? Thanks Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted January 20, 2011 Share Posted January 20, 2011 The first code only checks to see if the query executed without error. It may still have returned 0 rows. Quote Link to comment Share on other sites More sharing options...
PHPFAN10 Posted January 20, 2011 Author Share Posted January 20, 2011 The first code only checks to see if the query executed without error. It may still have returned 0 rows. Thanks, so basically to ensure user login credentials match database i need to do what i have done in second code to? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted January 20, 2011 Share Posted January 20, 2011 The first code only checks to see if the query executed without error. It may still have returned 0 rows. Thanks, so basically to ensure user login credentials match database i need to do what i have done in second code to? Yes, however you do it you need to check if the query returned a row. Quote Link to comment Share on other sites More sharing options...
PHPFAN10 Posted January 20, 2011 Author Share Posted January 20, 2011 Hi, Which my second lot of code does check if it returned a row as you can see Thanks, just needed clarification on it. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.