Connecting Tech Pros Worldwide Help | Site Map

When the condition is false the statment insdie false condition is not showing.

Member
 
Join Date: Oct 2007
Posts: 83
#1: 3 Weeks Ago
Dear All,

I have a simple login script, when the user put the correct information the loop is working but when they put wrong informtion the loop is not working. Kindly find the below code.

The statement after if($result<0) is not working.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("dbConfig.php");
  3. if(isset($_POST['login'])){
  4.     $userid = $_POST['userid'];
  5.     $userpassword = $_POST['password'];
  6.     $usertype = $_POST['usertype'];
  7.  
  8.     //$sql = mysql_query("SELECT * FROM logintable where UserID = '$userid'");
  9.     //if($sql){
  10.         $sql2 = mysql_query("SELECT * FROM logintable where UserID = '$userid' and UserPassword='$userpassword'");
  11.         //echo "h2i";
  12.         $result = mysql_num_rows($sql2) or die(mysql_error());
  13.         if($result<0){
  14.             echo "Sorry";
  15.             exit;
  16.         }
  17.         else{
  18.             while($row = mysql_fetch_array($sql2)){
  19.                 $usertype1 = $row['UserType'];
  20.                 $username =  $row['UserName'];
  21.                 $userlastlogin = $row['UserLastLoginDateTime'];
  22.                 $userlastip = $row['UserLastIP'];
  23.  
  24.                 echo $usertype1."-".$username."-".$userlastlogin."-".$userlastip;
  25.                 SESSION_START();
  26.                 $_SESSION['userid'] = $userid;
  27.                 $_SESSION['username'] = $username;
  28.                 echo $_SESSION['userid'];
  29.             }                                 
  30.         }
  31.     /*}
  32.     else{
  33.             echo "Sorry";
  34.     }*/
  35. }
  36. ?>
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: 3 Weeks Ago

re: When the condition is false the statment insdie false condition is not showing.


Well, your logic head is flawed. mysql_num_rows() will return the number of affected rows, if there are any. If there are 0 affected rows... yes, you guessed it, mysql_num_rows() will return 0.

So, the problem is that you're checking for a number less than 0 (which you will never have). You want to be check if mysql_num_rows() is equal to 0.

Mark.
Reply