Jump to content

Need help Negating Statement


doubledee

Recommended Posts

I want to negate the following statement so I can flip my THEN and ELSE...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)==1){
	// Member was Found.
}else{
	// Member Not Found.
	$errors['currPass'] = 'The E-mail and Password do not match those on file.';
}

 

 

Would this do the exact same thing...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)!==1){
	// Member Not Found.
	$errors['currPass'] = 'The E-mail and Password do not match those on file.';
}else{
	// Member was Found.
}

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/
Share on other sites

yes

 

though that leaves no room for more than 1 result - i assume that's not possible as you are searching for a member by id or some primary key, but just wanted to point it out.

 

from man page:

$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.

yes

 

though that leaves no room for more than 1 result - i assume that's not possible as you are searching for a member by id or some primary key, but just wanted to point it out.

 

Sorry. Some context.

 

I am trying to Log In a Member and looking for his/her records, so, yes, there should only ever be ZERO or ONE matches.

 

 

from man page:

$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.

 

Yeah, I saw that, but I don't feel confident about == vs === and != vs !==

 

 

Debbie

 

well for example

 

if ($x == $y)

will be true if $x is '0' and y is 0, even though one is a string and one an integer it is still the value - 0 - that counts.

 

=== will require the type to be the same as well, to be true.

 

the !, replaces one of the =

 

so !=, is the opposite of ==, and !== is the opposite of ===

When I check for a user record in the database, such as during login, I always check to see if the number of records returned is the same as I expect, which would be 1. If no records are returned, the wrong credentials have been entered, but if more than 1 record is returned, there's ambiguity in the data and that could potentially indicate a major problem.

 

// This assumes you've verified the query executed successfully, and the num_rows function did not return FALSE.
if( $records === 1 ) {
     // correct credentials, log the user in.
} elseif( $records === 0 ) {
     // incorrect credentials, return to login form, echo error message
} else {
     // The number of records returned was neither 1 nor 0. The script should be killed, the query logged, and an email sent to admin.
}

 

When I check for a user record in the database, such as during login, I always check to see if the number of records returned is the same as I expect, which would be 1. If no records are returned, the wrong credentials have been entered, but if more than 1 record is returned, there's ambiguity in the data and that could potentially indicate a major problem.

 

// This assumes you've verified the query executed successfully, and the num_rows function did not return FALSE.
if( $records === 1 ) {
     // correct credentials, log the user in.
} elseif( $records === 0 ) {
     // incorrect credentials, return to login form, echo error message
} else {
     // The number of records returned was neither 1 nor 0. The script should be killed, the query logged, and an email sent to admin.
}

 

Nice detailed answer!

 

So why do you use === vs == ?

 

I just have this...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)==1){
	// Member was Found.

 

 

Debbie

 

Because I know the num_rows function should return an integer if it succeeded, and I'm in the habit of making comparisons based on the tightest parameters I'm able to use. I.E. at that point, I already know my query executed successfully, so there's no reason to expect a FALSE from the num_rows function, which would cause num_rows == 0 to evaluate TRUE.

I want to negate the following statement so I can flip my THEN and ELSE...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)==1){
	// Member was Found.
}else{
	// Member Not Found.
	$errors['currPass'] = 'The E-mail and Password do not match those on file.';
}

 

 

Would this do the exact same thing...

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)!==1){
	// Member Not Found.
	$errors['currPass'] = 'The E-mail and Password do not match those on file.';
}else{
	// Member was Found.
}

 

 

Debbie

 

 

No, it doesn't.

 

The first is "is there one row". The second is "is the amount of rows not equal to one". In other words, the amount of rows may be 0, or 274... as long is there isn't exactly one row, it will be true.

 

If you want to know if NO rows are returned, you should do if(!mysqli_stmt_num_rows($stmt2)) or if(mysqli_stmt_num_rows($stmt2) == 0)

we covered that scootstah. she replied that there should never be more than one returned, and if so that's just as wrong as 0 ad needs to be fixed. there way more than one solution offered including yours, but that does seem to the the best.

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.