phpcodeigniteractiverecordescapingsql-function

CodeIgniter is not correctly applying identifier quotes in CONCAT_WS() expressions


Getting error when I tried like this:

$ci =& get_instance();              
$ci->db->select("CONCAT_WS(' ',users.name_first,users.name_last) AS user_name,CONCAT_WS(' ',advertisers.name_first,advertisers.name_last) AS advertiser_name,image,advertiser_reviews.last_updated");
            
$ci->db->join('users', 'users.id = advertiser_reviews.user_id');
$ci->db->join('advertisers', 'advertisers.id = advertiser_reviews.advertiser_id');
$ci->db->order_by('advertiser_reviews.id','desc');
$ci->db->limit(1);
$query = $ci->db->get('advertiser_reviews');

Error as follows:

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 (`hc_advertiser_reviews`) JOIN `hc_users` ON `hc_users`.`id` = `hc_advertis' at line 2

SELECT CONCAT_WS(' ', `hc_users`.`name_first`, `hc_users`.`name_last)` AS user_name, 
       CONCAT_WS(' ', `hc_advertisers`.`name_first`, `hc_advertisers`.`name_last)` AS advertiser_name, 
       `image`, 
       `hc_advertiser_reviews`.`last_updated` 
FROM (`hc_advertiser_reviews`) 
    JOIN `hc_users` ON `hc_users`.`id` = `hc_advertiser_reviews`.`user_id` 
    JOIN `hc_advertisers` ON `hc_advertisers`.`id` = `hc_advertiser_reviews`.`advertiser_id` 
ORDER BY `hc_advertiser_reviews`.`id` desc LIMIT 1

Solution

  • try to use $this->db->query("your sql query")

    EDIT: try to avoid the auto-quoting feature of the CodeIgniter DB class with

    $this->db->select("your-query",FALSE)