Php 在CodeIgniter中从数据库返回所有行

Php 在CodeIgniter中从数据库返回所有行,php,codeigniter,Php,Codeigniter,我试图从数据库中获取数据,并使用get_where比较一些字段,但每次都返回数据,即使条件不满足 我的代码是: function check_sec_token() { $sec_token=$this->session->userdata('sec_token'); $email_id=$this->session->userdata('email_id'); $query=$this->db->get_where($this->

我试图从数据库中获取数据,并使用get_where比较一些字段,但每次都返回数据,即使条件不满足

我的代码是:

function check_sec_token()
{
    $sec_token=$this->session->userdata('sec_token');
    $email_id=$this->session->userdata('email_id');
    $query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
    $rows=$query->num_rows();
    return $query->result();
    if($rows<1)
    {
        return false;
    }
    else
    {
        return true;
    }
}
它正在显示查询

Select * from user_reg where sec_token=0 and email_id=0
试试这个:

    ...

    if ($sec_token == NULL && $email_id == NULL)
    {
        $query=$this->db->get($this->_registration);
    }
    else
    {
        $query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
    }

    ...
注意:

return $query->result();
这段代码将停止运行其余代码!并且不考虑您的行计数条件

您可以这样做:

$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
if($rows<1)
{
    return false;
}
else
{
    return $query->result();
}
$query=$this->db->get_-where($this->\u注册,数组('sec\u-token'=>$sec\u-token,'email\u-id'=>$email\u-id));
$rows=$query->num_rows();
如果($rowsresult();
}

此处,如果$sec_token和$email_id为null或“0”,则查询您将获取所有记录。它将提供所有行,因为当“where条件不满足时,它将获取所有记录”试一试。还要检查会话是否启动,值是否设置正确。

所以你是在问条件是否正常……对吗?但亲爱的,我的问题是……如果条件不正确,它不应该返回所有记录……如果会话变量有空值,那么为什么它要添加0……是的,我知道……我只是将其用于测试…是否显示所有结果…但实际上我需要行数…如果条件不满足,它不应返回任何记录…即使条件不满足,它也会返回所有记录
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
if($rows<1)
{
    return false;
}
else
{
    return $query->result();
}