CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

DB_driver escape function suggested change.

Open aanbar opened this issue 13 years ago • 3 comments

The escape function is checking for boolean $str & retuning integers as 0/1 this proves to be problematic for MySQL as an int of value 0 equals any string, this can be resolved by returning quoted 0 & 1 instead of int.

elseif (is_bool($str))
        {
            return ($str === FALSE) ? 0 : 1;

to this one for example:

elseif (is_bool($str))
        {
            return ($str === FALSE) ? "'0'" : "'1'";

This would protect users from abnormal behaviour if by mistake a boolean is sent to ->where method.

aanbar avatar Sep 13 '13 16:09 aanbar

You said "mistake" yourself - that shouldn't happen and is the developer's responsibility. We're talking boolean here and that has nothing to do with strings.

If you try to create a field with type of BOOL or BOOLEAN in MySQL, it will create a TINYINT(1) field + the manual itself says it:

However, the values TRUE and FALSE are merely aliases for 1 and 0, respectively.

Reference: http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html

narfbg avatar Sep 13 '13 16:09 narfbg

Sure, I know

My point was a quoted zero or one won't affect the select nor insert (I am talking MySQL) but an un-quoted zero would for sure affect the select if thrown at a string by mistake.

->where('stringField', FALSE)

would match all the rows in a table, if someone by mistake thrown it in a deleted statement it would be a catastrophe.

As you said it's developer responsibility to make sure what data he passes through, but my point is if it's confirmed (by other DB experts) that changing these into strings won't break things, then it would be better to do it just like the 'affected rows hack for mysql delete' it's a hack to deal with weird behaviour.

aanbar avatar Sep 13 '13 16:09 aanbar

So, basically you're saying that we should try to protect people who try to do the following:

$this->db->where('<string type field>', FALSE)
         ->delete('<table name>');

I don't think that this can happen by accident, and honestly - they deserve it if they do. :)

narfbg avatar Sep 13 '13 16:09 narfbg