Welcome Guest, Not a member yet? Register   Sign In
DATE_FORMAT in active record class
#1

[eluser]mikeymayhem[/eluser]
hey ive seen another thread on this topic but the posts there dont seem to be working for me can anyone shed some light on this topic i want to format the date like
this->db->select("DATE_FORMAT(post_date, '%W %M, %Y') AS date"); the field in the database is called post_date

Here's my method in my model
Code:
function get_gigs( $num,$offset ) {
        $this->db->select('*');
        $this->db->select("DATE_FORMAT(post_date, '%W %M, %Y') AS date");
        $this->db->from('gigs');
        $this->db->join('venues', 'venues.venue_id = gigs.venue_id');
        $this->db->join('users' , 'users.user_id = gigs.user_id');
        $this->db->where('active', '1');
        $this->db->order_by('post_date');
        $this->db->limit($num, $offset);
        $query = $this->db->get();

        return $query;
    }
im getting this error
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (`gigs`) JOIN `venues` ON `venues`.`venue_id` = `gigs`.`venue_id` JOIN `use' at line 2

tahnks in advance, guess its something stupid im missing
#2

[eluser]Deveron[/eluser]
Hi mike,

there is a problem with escaping.
To avoid it just put as secound parameter "FALSE" in the select.

Code:
$this->db->select("DATE_FORMAT(post_date, '%W %M, %Y') AS date", FALSE);
#3

[eluser]LinkFox[/eluser]
Or just manually write the query..
Code:
$sql = "DATE_FORMAT(BLAHBLAH) as date FROM BLAH WHERE BLAH";

$result = $this->db->query($sql);
#4

[eluser]mikeymayhem[/eluser]
[quote author="deveron" date="1290021453"]Hi mike,

there is a problem with escaping.
To avoid it just put as secound parameter "FALSE" in the select.

Code:
$this->db->select("DATE_FORMAT(post_date, '%W %M, %Y') AS date", FALSE);
[/quote]

Cheers man that worked a treat. i wanted to avoid writing the query manually. thanks for the quick response.
#5

[eluser]mikeymayhem[/eluser]
just out of interest what does specifying the second param as FALSE actually do?
#6

[eluser]Deveron[/eluser]
As described in the user guide:


$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.
#7

[eluser]mikeymayhem[/eluser]
hahaha yeah just checked the user guide!! cheers!!!




Theme © iAndrew 2016 - Forum software by © MyBB