Welcome Guest, Not a member yet? Register   Sign In
Help with this SELECT statement
#5

[eluser]RobertSF[/eluser]
[quote author="jerry4christ" date="1413527098"]Yes, you got it and see exactly what I see. But that's actually, my question, what is wrong with the codeigniter query? That's what I don't understand [/quote]

I don't think you're doing anything wrong. It looks like the Codeigniter select method in the active record object doesn't expect more than field names separated by commas. Here's the code:

Code:
68:      * Select
69:      *
70:      * Generates the SELECT portion of the query
71:      *
72:      * @param   string
73:      * @return  object
74:      */
75:     public function select($select = '*', $escape = NULL)
76:     {
77:         if (is_string($select))
78:         {
79:             $select = explode(',', $select);
80:         }
81:
82:         foreach ($select as $val)
83:         {
84:             $val = trim($val);
85:
86:             if ($val != '')
87:             {
88:                 $this->ar_select[] = $val;
89:                 $this->ar_no_escape[] = $escape;
90:
91:                 if ($this->ar_caching === TRUE)
92:                 {
93:                     $this->ar_cache_select[] = $val;
94:                     $this->ar_cache_exists[] = 'select';
95:                     $this->ar_cache_no_escape[] = $escape;
96:                 }
97:             }
98:         }
99:         return $this;
100:     }

In line 79, it explodes the select argument by commas, which works just fine when you do, for example:
Code:
$this->db->select('name, address, city, state, zip, phone, email')
but when you use
Code:
$this->db->select("SUM(IF(comments.is_active = 1,1,0)) total_comments")
looks like it doesn't expect the commas in the IF statement.

The Codeigniter documentation says that if you send the select method an optional second parameter with the value FALSE, that the method won't then try to protect your field names with backticks. That may work. There is also a select_sum method that may do the trick. If not, just write the query out in full.


Messages In This Thread
Help with this SELECT statement - by El Forum - 10-16-2014, 11:38 AM
Help with this SELECT statement - by El Forum - 10-16-2014, 10:04 PM
Help with this SELECT statement - by El Forum - 10-16-2014, 11:24 PM
Help with this SELECT statement - by El Forum - 10-17-2014, 10:17 AM
Help with this SELECT statement - by El Forum - 10-17-2014, 11:21 AM
Help with this SELECT statement - by El Forum - 10-17-2014, 01:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB