CodeIgniter Forums
Problem Using Active Record with MySQL DATE_FORMAT command - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem Using Active Record with MySQL DATE_FORMAT command (/showthread.php?tid=47711)



Problem Using Active Record with MySQL DATE_FORMAT command - El Forum - 12-19-2011

[eluser]LynxCoder[/eluser]
Hi Guys,
I have the following code:

$this->db->select("imageid,imagedate,DATE_FORMAT(imagedate, %d-%m ) AS thisday, DATE_FORMAT(imagedate, '%Y' ) AS imageyear");
$this->db->from('images');
$this->db->where("DATE_FORMAT(imagedate, '%d-%m') = ", $usedate);
$this->db->where("imagedate <", $thisyear."-01-01");
$this->db->order_by('imageid desc');
$query=$this->db->get();

The SQL works fine when put into phpMyAdmin and executed as a pure SQL statement (admittedly without the CI Active Record code) but when I execute the above I get the following problem:



A Database Error Occurred
Error Number: 1054

Unknown column '"%d-%m"' in 'field list'

SELECT `imageid`, `imagedate`, DATE_FORMAT(imagedate, `"%d-%m"` ) AS thisday, DATE_FORMAT(imagedate, `"%Y"` ) AS imageyear FROM (`images`) WHERE DATE_FORMAT(imagedate, "%d-%m") = '19-12' AND `imagedate` < '2011-01-01' ORDER BY `imageid` desc

Now CI is adding extra `` around the date string. i've tried removeing the "" marks and still get the same error. CI is leading MySQL to think that %d-%m is a field name. Any thoughts on how I can get around it ... spent three hours playing with this today!

Cheers
Rich