phpcodeigniterselectactiverecordwhere-clause

CodeIgniter active record SELECT query with operator in where() calls returns no rows and no errors


I want to select and then var_dump the database rows, but I can't since var_dump is returning the bool(false).

My model: (players.php):-

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Players extends CI_Model
{
    private $table_name = 'players';

    function __construct()
    {
        parent::__construct();
    }
    
    function get($id = FALSE)
    {
        $this->db->select();
        $this->db->where('ban <', time());
        if ( $id )
            $this->db->where('id =', $id);

        $query = $this->db->get($this->table_name);
        return $query->num_rows() == 0;
    }
}

/* End of file test.php */
/* Location: ./application/models/test.php */

My player.php controller:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Player extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('tank_auth');
    }

    public function index()
    {
        $this->load->model('players');
        $this->load->view('template/header');
        $this->load->view('default');
        var_dump($this->players->get());
        $this->load->view('template/footer');
    }
}

/* End of file player.php */
/* Location: ./application/controllers/player.php */

Have you any solutions for that?


Solution

  • $this->db->where('id =', $id);
    

    is incorrect

    $this->db->where('id', $id);
    

    is correct. You don't need the = operand with a normal WHERE =; use operands for <, >, !=

    Also,

    return $query->num_rows() == 0; 
    

    will return bool false if you have more than 0 rows (i.e. if you have any users with ban < time(). Try returning just $query and var_dump'ing that.