0

I'm having problems when I try to insert a function of Select detro example DATE_FORMAT (date_end, ‘% W% M% Y’) AS dateend.

$this->db->select(name, DATE_FORMAT(date_end, ‘% W% M% Y’) AS dateend)

It says the ‘% W% M% Y’ method is unknown.

2 Answers 2

3

According to this post and this forum entry, adding false to your query will do the job:

$this->db->select("DATE_FORMAT(your_date_field, 'Year: %Y Month: %m Day: %d') AS formated_date", FALSE );
Sign up to request clarification or add additional context in comments.

1 Comment

I was curious to know what the false do. According to the documentation : If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks..
0

First off, those curly quotes around your date format expression look problematic. ‘% W% M% Y’

  • Double quoted string with single quoted date format expression will work, but not all identifiers will be quoted and the hard-coded single quotes may not be portable/suitable to other SQL dialects.

    $this->db->select("name, DATE_FORMAT(date_end, '%W %M %Y') AS dateend");
    

    renders as:

    SELECT name, DATE_FORMAT(date_end, '%W %M %Y') AS dateend
    
  • Turning off escaping via false as the second parameter of select() is the same as above but removes the magic quoting on the name column. This will also work for this specific scenario, but there are no protections against potential columns which share a name with reserved keywords.

    $this->db->select("name, DATE_FORMAT(date_end, '%W %M %Y') AS dateend", false);
    

    renders as:

    SELECT name, DATE_FORMAT (date_end, '% W% M% Y') AS dateend
    
  • For full and portable quoting across all column names and values, turn off the automated escaping and explicitly apply quoting to all identifiers and values in the SELECT clause.

    $this->db->select(
        sprintf(
            '%1$s, DATE_FORMAT(%2$s, %3$s) AS %2$s',
            $this->db->escape_identifiers('name'),    // %1$s
            $this->db->escape_identifiers('date_end'),// %2$s
            $this->db->escape('%W %M %Y')             // %3$s
        ),
        false
    )
    

    renders as:

    SELECT `name`, DATE_FORMAT(`date_end`, '%W %M %Y') AS `date_end`
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.