-2

This is my code:

   <?php

    $host="localhost";
    $user="root";
    $pass="";
    $db="comportal";

    mysql_connect($host, $user,$pass);
    mysql_select_db($db);


    if(isset($_POST['username'])){
    $username=$_POST['username'];
    $password=$_POST['password'];
    $sql="SELECT * FROM user WHERE username='".$username."' AND password='".$password."' LIMIT 1";
    $res= mysql_query($sql);

    if(mysql_num_rows($res)==1){
      echo "Successfully logged in.";
      exit();
     }
     else{
       echo "Invalid Information. Please return to the previous page.";
       exit();
     }
    }
    ?>

and I always get this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sample\login.php on line 20
Invalid Information. Please return to the previous page.

Then I tried changing

  if(mysql_num_rows($res)==1){
  echo "Successfully logged in.";
  exit();

with

  if($res==true){
  echo "Successfully logged in.";
  exit();

But, now it's logically wrong. it says successfully logged in even though that is not stored in the database.

please how could I resolve that?

Tarik
  • 4,961
  • 3
  • 36
  • 67
Jab Griffin
  • 61
  • 1
  • 4
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) –  Feb 17 '15 at 23:12

1 Answers1

-1

From w3resource.com:

Query handle for successful SELECT, SHOW, DESCRIBE, EXPLAIN and other statements or FALSE on error.

So while your query may not be returning any rows it is still a successful select query,hence why you are getting true for if($res==true).

The reason you're getting an error for if(mysql_num_rows($res)==1) is because you've assigned a boolean value to it with $res= mysql_query($sql); not an int as required.

Barry O'Kane
  • 1,189
  • 7
  • 12