我已经尝试过此代码来计算行数
$count = $this->db->query("select count(*) from cgdict where cg like '%".$search_data."%'");
echo $count;
它给了我一个错误
Object of class CI_DB_mysqli_result could not be converted to string
使用 num_rows()
来计数函数,因为$count
是一个数组(如果不是 null):
$query = $this->db->query("select * from cgdict where cg like '%".$search_data."%'");
$result = $query->result_array()
$num= $result ->num_rows();
echo $num;
或
$query = $this->db->query("select count(*) as TotCount from cgdict where cg like '%".$search_data."%'");
$result = $query->result_array()
echo count($result[0]['TotCount']);
安全说明
在将$search_data
直接发送到 SQL 查询之前,请确保使用 $this->input->post
来捕获和清除 SQL 注入。