80后奔三ing 发表于 2012-5-3 10:47:30

num_rows() 与直接执行sql语句返回结果不一致。

本帖最后由 80后奔三ing 于 2012-5-3 11:34 编辑

同一条SQL语句,通过
$query_count = $this->db->query($sql_count)->num_rows();
得到的结果是1

而直接在数据库里执行SQL语句得到的结果是2。

这是什么原因呀?

model里的代码如下:

$sql_count = "select count(*) from `employee` where `delete` = 0";
$sql = "select `id`,`department_id`,`employee_class_id`,`name`,`turename`,`phone`,`register` from `employee` where `id` <= (select `id` from `employee` order by `id` desc limit ".ceil($data['segment']) * $data['per_page'].") and `delete` = 0";
$query_count = $this->db->query($sql_count)->num_rows();
$query = $this->db->query($sql);
return$query->result_array();


我是想要做一个分页。。

太尉天上飞 发表于 2012-5-3 10:58:01

$sql_count = "select count(*) as count from `employee` where `delete` = 0";
                $row = $this->db->query($sql_count)->get()->row_array();
                $query_count = $row['count'];

80后奔三ing 发表于 2012-5-3 11:30:59

试了下,LS上方法,报错。。
get()
方法不存在。。。

80后奔三ing 发表于 2012-5-3 11:33:52

我知道问题所在了。。。
num_rows()是返回结果的行数。。。

$sql_count = "select count(*) as count from `employee` where `delete` = 0";

这个执行后的结果肯定是只返回一行。。。

谢谢太尉天上飞

kissgxd 发表于 2012-5-3 11:50:29

count(*) 返回的记录条数是1,他本身的值是2,楼主如果想用num_rows就把count去掉吧

80后奔三ing 发表于 2012-5-3 14:58:39

kissgxd 发表于 2012-5-3 11:50 static/image/common/back.gif
count(*) 返回的记录条数是1,他本身的值是2,楼主如果想用num_rows就把count去掉吧 ...

嗯,谢谢啊!!
页: [1]
查看完整版本: num_rows() 与直接执行sql语句返回结果不一致。