Search code examples
mysqlcodeigniteractiverecordcodeigniter-2

DATE_FORMAT within a query in CodeIgniter using Active Record don't work


coders. I'm running a little problem here and can't find the solution. I'm building a query using Active Record from CI. This is the code for the query:

 $this->db->select("u.id AS user_id, u.email, p.display_name, p.first_name, p.last_name, s.status_id, s.message");
    $this->db->select("DATE_FORMAT(s.created_at, `Publicado el %d/%m/%Y a las %h:%i %p`) AS created_at", FALSE);

 $this->db->join($this->_table_users . ' u', 'u.id = s.user_id');
 $this->db->join($this->_table_profiles . ' p', 'p.user_id = u.id');

 $this->db->where_in('user_id', $str);
 $this->db->where('deleted', 0);

 $this->db->from($this->_table . ' s');

 $this->db->order_by('s.created_at', 'desc');

But I'm getting this error:

Error Number: 1054

Unknown column 'Publicado el %d/%m/%Y a las %h:%i %p' in 'field list'

SELECT u.id AS user_id, u.email, p.display_name, p.first_name, p.last_name, s.status_id, s.message, DATE_FORMAT(s.created_at, Publicado el > %d/%m/%Y a las %h:%i %p) AS created_at FROM (default_status s, default_status) JOIN > default_users u ON u.id = default_s.user_id JOIN default_profiles p ON p.user_id = u.id WHERE user_id IN ('5726, 2, 10293') AND deleted = 0 ORDER BY s.created_at desc LIMIT 2

Does any know where I can use DATE_FORMAT within this query? The funny is the same query write as pure SQL works Cheers and thanks in advance


Solution

  • Add FALSE as the second parameter in your SELECT statement. You may have to do something similar in your WHERE clause as well.

    Some links to look over:

    http://ellislab.com/forums/viewthread/74206/#368778

    http://ellislab.com/forums/viewthread/206962/#963061

    http://ellislab.com/codeigniter/user-guide/helpers/date_helper.html