doubledee Posted February 4, 2012 Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/ Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314539 Share on other sites More sharing options...
Danny696 Posted February 4, 2012 Share Posted February 4, 2012 It should work, usually NOT EQUAL statements are just !=, but I do believe !== will work, but you could also do if (mysqli_stmt_num_rows($stmt2) == 0){ OR just a if (!mysqli_stmt_num_rows($stmt2)){ Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314540 Share on other sites More sharing options...
doubledee Posted February 4, 2012 Author Share Posted February 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314544 Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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 === Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314550 Share on other sites More sharing options...
Psycho Posted February 4, 2012 Share Posted February 4, 2012 if (!mysqli_stmt_num_rows($stmt2)){ ^^ That is what I would use. 0 is treated as the logical 'false', so if 0 records are returned that condition will be true. Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314560 Share on other sites More sharing options...
Pikachu2000 Posted February 5, 2012 Share Posted February 5, 2012 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. } Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314598 Share on other sites More sharing options...
doubledee Posted February 5, 2012 Author Share Posted February 5, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314642 Share on other sites More sharing options...
Pikachu2000 Posted February 5, 2012 Share Posted February 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314651 Share on other sites More sharing options...
scootstah Posted February 5, 2012 Share Posted February 5, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314673 Share on other sites More sharing options...
digibucc Posted February 5, 2012 Share Posted February 5, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256411-need-help-negating-statement/#findComment-1314750 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.