Skip to content
Advertisement

How to count the number of rows with a date from a certain year in CodeIgniter?

I have the following query.

$query = $this->db->query('SELECT COUNT(*) FROM iplog.persons WHERE begin_date LIKE '2014%'');

I need to count the number of columns with a begin_date in the year 2014.

When I run this script I’m getting an error:

Parse error: syntax error, unexpected ‘2014’ (T_LNUMBER) in C:xampphtdocsiPlog2applicationcontrollersstat.php on line 12

I was trying to change my CI script to

$query = $this->db->query('SELECT COUNT(*) FROM iplog.persons WHERE begin_date LIKE "2014%"');

but it caused an error.

Advertisement

Answer

You mean, count ROWS:

So for that, just count the number of rows you have based on a condition:

$year = '2014'
$this->db->from('iplog');
$this->db->like('begin_date', $year); 
$query = $this->db->get();
$rowcount = $query->num_rows();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement