Code Igniter选择模型中的活动记录不起作用

时间:2012-02-22 20:07:28

标签: codeigniter activerecord select

我想选择然后var_dump数据库行,但我不能var_dump返回bool(false)

我的模特:(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 */

我的player.php控制器:

<?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 */

你有任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

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

不正确

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

是对的。您不需要具有正常WHERE =的=操作数;使用<>!=

的操作数

另外,

return $query->num_rows() == 0; 
如果您的行数超过0行,

将返回bool false(例如,如果您有ban < time()的用户。请尝试仅返回$queryvar_dump。< / p>