Skip to content
Advertisement

How to count no of row with like command in Codeignighter

I have tried this code for count rows

$count = $this->db->query("select count(*) from cgdict where cg like '%".$search_data."%'");

echo $count;

and it gives me an error

Object of class CI_DB_mysqli_result could not be converted to string

Advertisement

Answer

Use num_rows() to count function, because $count is an array (if not null):

$query = $this->db->query("select * from cgdict where cg like '%".$search_data."%'");
$result = $query->result_array()
$num= $result ->num_rows();
echo $num;

Or

$query = $this->db->query("select count(*) as TotCount from cgdict where cg like '%".$search_data."%'");
$result = $query->result_array()

echo count($result[0]['TotCount']);

Security note

Before sending $search_data direct to SQL query, make sure you use $this->input->post to catch and clean from SQL injection.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement