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
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.
Link to comment
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.

 

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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