3

please i have an issue with codeigniter. when i try to log here is the result:

Fatal error: Call to a member function num_rows() on boolean in D:\xampp\htdocs\procurementSys\application\models\login_model.php on line 19

Below tho code of the relative file:

  <?php


  class Login_model extends CI_Model {

//this function checks whether the username and the password is in the database or not
public function check_login($username, $password){

    $this->db->select('username, password, status');
    $array = array('username' => $username, 'password' => sha1($password),'status' => 'active');
    $this->db->where($array);
    $query = $this->db->get('user');

    if($query->num_rows() == 1) // if the affected number of rows is one
    {
        return true;
    }
    else
    {
        return false;
    }
}

//this function returns the status of the user to be used in authentication
public function user_login_data($username, $password){

    $this->db->select('status');
    $array = array('username' => $username, 'password' => sha1($password));
    $this->db->where($array);
    $query = $this->db->get('user');

    if($query->num_rows() == 1) // if the affected number of rows is one
    {
         $row = $query->row();
         return $row->status;
    }
  //        else
  //        {
  //            return false;
  //        }
}


public function user_role($username){ // this function gets the user's role from the database
     $this->db->select('role');
     $this->db->where('username', $username);
     $query = $this->db->get('user');
     $row = $query->row(0);

     return $row->role;
}

public function department($username){ // this function gets the user's department from the database
     $this->db->select('department');
     $this->db->where('username', $username);
     $query = $this->db->get('user');
     $row = $query->row(0); // returns the first row with an array of objects that is stored in the row variable

     return $row->department;
}

public function get_user_id($username){ // this function gets the user's department from the database
     $this->db->select('userID');
     $this->db->where('username', $username);
     $query = $this->db->get('user');
     $row = $query->row(0); // returns the first row with an array of objects that is stored in the row variable

     return $row->userID ;
}

public function fullname($username){
     $this->db->select('firstName, secondName');
     $this->db->where('username', $username);
     $query = $this->db->get('user');
     $row = $query->row(0);
     return $row;

  //            foreach($query->result() as $row) // returns the query as an array of objects
  //           {
  //                $data[] = $row; // equates the array of objects to an array variable
  //           }
  //        
  //           return $data;
   // }
}

 }

 ?>

I kept searching for a solution and found this post (Call to a member function num_rows() on boolean) . It gave me an idea but no real help. thanks

Community
  • 1
  • 1
referag
  • 31
  • 1
  • 1
  • 6
  • plz mention error in which function ???? – devpro Feb 25 '16 at 14:45
  • It seems that the variable `$query` that you are trying to use an an object and call a method on is in fact a boolean, not an object. You can just use var_dump to see for your self, to debug it. In your case this usually means that the query above failed, so you should research error handling. – mishu Feb 25 '16 at 14:46
  • indeed, var_dump@$query returned BOOL(FALSE). how to solve that. what should i include as code to help me cheking the errors? – referag Feb 25 '16 at 16:12
  • `db->get()` can return FALSE which usually indicates a problem with the sql statement. For me it is usually something in the `where` part of the statement. I have gotten in the habit of using `if($query){..` before using it, eg. `$row = $query->row();` – DFriend Feb 25 '16 at 16:34
  • public function user_login_data($username, $password) { $this->db->select('status'); $array = array( 'username' => $username, 'password' => sha1($password) ); $this->db->where($array); $query = $this->db->get('user'); if ($query) { if ($query->num_rows() == 1) { $row = $query->row(); return $row->status; } else { return false; } } else { return false; } } – Tauqueer Hassan Jun 26 '19 at 05:14

5 Answers5

4

I got this error with MySQL Strict Mode enabled. When I disable Strict Mode, error disappeared.

To check this mode is already enabled

SHOW VARIABLES LIKE 'sql_mode';

To make it disabled

SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';
Rajitha Bandara
  • 743
  • 1
  • 10
  • 16
  • I think this is a temporary solution. When we restart mysql again we will face the same issue. – paul Dec 15 '22 at 19:33
1

Something like this for the count:

$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
safin chacko
  • 1,345
  • 1
  • 11
  • 18
0

**Try something like this **

//this function checks whether the username and the password is in the database or not


public function check_login($username, $password){

$this->db->select('username, password, status');
$array = array('username' => $username, 'password' => sha1($password),'status' => 'active');
$this->db->where($array);
$query = $this->db->get('user');
$result = $query->result();

if($result->num_rows() == 1) // if the affected number of rows is one
{
    return true;
}
else
{
    return false;
}
}
Deb Sharma
  • 114
  • 5
0

CODEIGNITER send boolean as result if there is not result for example as per your method

public function check_login($username, $password){

$this->db->select('username, password, status');
$array = array('username' => $username, 'password' => sha1($password),'status' => 'active');
$this->db->where($array);
$query = $this->db->get('user');

if($query->num_rows() == 1) // if the affected number of rows is one
{
    return true;
}
else
{
    return false;
}

If there is no data in table it send False while we are expecting some object on which we can work so before calling $query->num_rows() check if it is an object or not so code can be like :

 $query = $this->db->get('user');
    if(is_object($query)){
        if($query->num_rows() == 1) // if the affected number of rows is one
    {
        return true;
    }
        }else{  
        return false;
        }
Abhinav bhardwaj
  • 2,657
  • 25
  • 21
0

You can write like that. It will work smoothly.

if($query !== FALSE && $query->num_rows() == 1) // if the affected number of rows is one
{
    return true;
}
else
{
    return false;
}
Tauqueer Hassan
  • 217
  • 2
  • 5