Jump to content

Am i creating more code than needed ?


PHPFAN10

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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?

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.