Codeigniter查询 - > num_rows返回0不正确

时间:2014-01-06 18:14:06

标签: php codeigniter

我似乎无法通过以下查询运行并正确返回num_rows。无论发生什么,$query->num_rows > 0总是会返回false,即使我希望它返回true。有什么想法吗?

    $post_id = $this->input->post('post_id');
    $poster_id = $this->input->post('poster_id');
    $my_id = $this->session->userdata('id');

    $query = $this->db->query("SELECT * FROM default_post_likes
                               WHERE liker_id = '$my_id'
                               AND post_id = '$post_id'");

    if ($query->num_rows > 0) {
        echo json_encode(array('error' => 'e1'));
    } else {
        $data = array(
            'poster_id' => $poster_id,
            'post_id' => $post_id,
            'liker_id' => $my_id
        );

        $this->db->insert('default_post_likes', $data);

        echo json_encode(array('success' => true));
    }

2 个答案:

答案 0 :(得分:6)

$query->num_rows应该是一个函数$query->num_rows()

答案 1 :(得分:0)

这里,问题在于

if ($query->num_rows > 0) {
    echo json_encode(array('error' => 'e1'));
}

$query->num_rows  (Wrong)

应该是一个函数,因此你必须将其用作

$query->num_rows() (Right)

这就是为什么它返回False