Php CI-num_行不返回可读数字

Php CI-num_行不返回可读数字,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,使用CodeIgniter 3和num_rows()不会使用return返回可读数字(为空)。让它显示实际数字的唯一方法是使用print或echo——但这破坏了我需要的功能 public function count_notifications($user_id) { $query = $this->db->select("*") ->where("user_id", $user_id) ->

使用CodeIgniter 3和
num_rows()
不会使用
return
返回可读数字(为空)。让它显示实际数字的唯一方法是使用
print
echo
——但这破坏了我需要的功能

    public function count_notifications($user_id)
    {
        $query = $this->db->select("*")
            ->where("user_id", $user_id)
            ->order_by("time_received", "desc")
            ->get("members_notifications")
            ->num_rows();

        return $query;
    }
为什么会这样?

num\u rows()
将始终返回某个整数,无论是
int(0)
(无结果)还是
int(4)
。我运行了我自己的测试,这与你的测试非常相似,因为有一秒钟我认为这是可能的(事实并非如此)

您应该经历典型的调试过程。我能想到的唯一一件事是,您的语法或措辞中有一个错误,导致
get()
返回布尔值(可能您没有看到,因为您已经完成了错误报告)

我不确定您是如何验证函数是否没有返回整数的,但可能是从那里开始的,因为您的逻辑可能存在问题。
num\u rows()
将始终返回某个整数,无论是
int(0)
无结果还是
int(4)
。我运行了我自己的测试,这与你的测试非常相似,因为有一秒钟我认为这是可能的(事实并非如此)

您应该经历典型的调试过程。我能想到的唯一一件事是,您的语法或措辞中有一个错误,导致
get()
返回布尔值(可能您没有看到,因为您已经完成了错误报告)

我不确定您是如何验证函数是否没有返回整数的,但可能从那里开始,因为您的逻辑可能存在问题。尝试一下

public function count_notifications($user_id)
{
    $query = $this->db->where("user_id", $user_id)
             ->get("members_notifications");            
    return $query->num_rows();
}
我真的不希望它与您的代码有所不同。但您可以轻松确认
$query
CI\u DB\u结果还是bool。

尝试一下

public function count_notifications($user_id)
{
    $query = $this->db->where("user_id", $user_id)
             ->get("members_notifications");            
    return $query->num_rows();
}

我真的不希望它与您的代码有所不同。但您可以轻松确认
$query
CI\u DB\u结果还是bool。

尝试使用以下方法:

public function count_notifications($user_id)
{
     $query = $this->db->select("*")
            ->where("user_id", $user_id)
            ->order_by("time_received", "desc")
            ->get("members_notifications");

    return $query->num_rows();
}

尝试使用以下方法:

public function count_notifications($user_id)
{
     $query = $this->db->select("*")
            ->where("user_id", $user_id)
            ->order_by("time_received", "desc")
            ->get("members_notifications");

    return $query->num_rows();
}

预期的行为是什么?@Ibu返回它能找到的行数。目前它不返回任何内容。这是在黑暗中拍摄,但是您是否尝试过
return(int)$query
?@Ibu,
num_rows()
返回一个int。预期的行为是什么?@Ibu返回它能找到的行数。目前它不返回任何内容。这是在黑暗中拍摄,但是您是否尝试过
return(int)$query
?@Ibu,
num\u rows()
返回一个int。