CodeIgniter Forums
DB_active_rec.php, function: select(), issue: cannot use MySQL IF function (not construct) + more. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: DB_active_rec.php, function: select(), issue: cannot use MySQL IF function (not construct) + more. (/showthread.php?tid=6170)



DB_active_rec.php, function: select(), issue: cannot use MySQL IF function (not construct) + more. - El Forum - 02-17-2008

[eluser]MPress[/eluser]
You cannot use the MySQL IF function within the string argument for the select active-record function. This is because it tries to reconcile the values by exploding on a ','. Although, I somehow remember this not being an issue previously... Anyways, the IF function has the syntax of: IF({cond. is met}, {return this}, {otherwise this}).

Update:

As well, the new automatic backquote feature of AR functions fails on this kind of thing: FORMAT(something, 2) since we get FORMAT(`something`, `2`). 2 must be numeric and therefore cannot be enclosed in backquotes.

Here is a temporary workaround for the first issue. Simply escape special commas by doubling up (',,'). Follow $comma_fix.
Code:
function select($select = '*', $protect_identifiers = TRUE, $comma_fix = FALSE)
    {
        if (is_string($select))
        {
            if($comma_fix && strpos($select, ',,') !== FALSE){

                $select = str_replace(',,', '?~', $select);
            }

            $select = explode(',', $select);
        }

        foreach ($select as $val)
        {
            $val = trim($val);

            if($comma_fix && strpos($val, '?~') !== FALSE){

                $val = str_replace('?~', ',', $val);
            }

            if ($val != '*' && $protect_identifiers !== FALSE)
            {
                if (strpos($val, '.') !== FALSE)
                {
                    $val = $this->dbprefix.$val;
                }
                else
                {
                    $val = $this->_protect_identifiers($val);
                }
            }

            if ($val != '')
            {
                $this->ar_select[] = $val;
                if ($this->ar_caching === TRUE)
                {
                    $this->ar_cache_select[] = $val;
                }
            }
        }
        return $this;
    }



DB_active_rec.php, function: select(), issue: cannot use MySQL IF function (not construct) + more. - El Forum - 02-17-2008

[eluser]Seppo[/eluser]
Have you tried passing $protect_identifiers = FALSE?


DB_active_rec.php, function: select(), issue: cannot use MySQL IF function (not construct) + more. - El Forum - 02-17-2008

[eluser]Derek Allard[/eluser]
Yes, I think this might just be a matter of needing
Code:
$this->db->select("query", FALSE);
but if you could show me the minimum of code needed to recreate your bug I could provide some better insight.