如何在Codeignighter中使用类似的命令计算行数



我已经尝试过此代码来计算行数

$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 注入。

最新更新