Part of the EllisLab Network
   
 
CI 1.7:if dbprefix is set, “$this->db->select” is trying to add dbprefix to its one of mysql funtion parameter.
Posted: 01 November 2008 12:18 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  10
Joined  09-06-2008

(first, i’m not an english user. so, please be tolerant about my english. wink)

i’m using CI 1.7.0 and making a bulletin board system with it.
i set dbprefix “m_” in “system/application/config/database.php” and wrote some codes using Active Record Class to get a list of articles.
codes are like this:

$this->db->select("A.id, A.virtualnumber, A.author, A.subject, COUNT(CM.id) AS commentnumbering, A.count, DATE(A.created) AS created");
$this->db->from("board_article AS A");
$this->db->join("board_config AS C""A.boardid = C.id");
$this->db->join("board_comment AS CM""A.id = CM.articleid""LEFT");
$this->db->where("C.identifier"$boardIdentifier);
$this->db->group_by("A.id");
$this->db->order_by("A.created""DESC");
$this->db->limit($offset$limit);
$ret $this->db->get(); 

and, i expected query like this:

SELECT A.idA.virtualnumberA.authorA.subjectCOUNT(CM.id) AS commentnumberingA.countDATE(A.created) AS created
FROM m_board_article 
AS A
JOIN m_board_config 
AS C ON A.boardid C.id
LEFT JOIN m_board_comment 
AS CM ON A.id CM.articleid
WHERE C
.identifier "notice"
GROUP BY A.id
ORDER BY A
.created DESC
LIMIT 1
10

but i couldn’t get any results from that. so i debugged it using Profiler Class.
Profiler said:

SELECT `A`.`id`, `A`.`virtualnumber`, `A`.`author`, `A`.`subject`, `m_COUNT(CM`.`id)` AS commentnumbering, `A`.`count`, `m_DATE(A`.`created)` AS created
FROM 
(`m_board_article` AS A)
JOIN `m_board_config` AS C ON `A`.`boardid` = `C`.`id`
LEFT JOIN `m_board_comment` AS CM ON `A`.`id` = `CM`.`articleid`
WHERE `C`.`identifier` = 'notice'
GROUP BY `A`.`id`
ORDER BY `A`.`createdDESC
LIMIT 1
10 

you can find what is wrong with this query. see SELECT clause. CI db class added dbprefix to mysql internal function. “m_COUNT(CM.id) AS commentnumbering” and “m_DATE(A.created) AS created” is it.

is it a bug or not?
am i missing something while i’m using this Active Record Class?

thanks.

 Signature 

CI saves me!

Profile
 
 
Posted: 01 November 2008 12:30 PM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRank
Total Posts:  3097
Joined  01-07-2008

What you need here is the second parameter set to FALSE:

$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.

 Signature 
Profile
MSG
 
 
Posted: 01 November 2008 12:47 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  10
Joined  09-06-2008
inparo - 01 November 2008 04:30 PM

What you need here is the second parameter set to FALSE:

$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.

i know $this->db->select() has second parameter. i know there are some alternative ways to avoid what i have experienced. but, $this->db->select() is not supposed to add dbprefix to mysql internal functions that are used as its parameters, is it? and, if i could not use these Active Record Class to protect fields by auto escaping or something like that, what is that for?

plus, that solution didn’t work. it still adds dbprefix.

 Signature 

CI saves me!

Profile
 
 
Posted: 02 July 2009 06:11 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  4
Joined  07-01-2009
Pascal Kriete - 01 November 2008 04:30 PM

What you need here is the second parameter set to FALSE:

$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.

Thanks…worked perfectly!

Profile