Unable to get/set BIT value
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
chive |
Fix Released
|
High
|
David Roth |
Bug Description
Try to define column as BIT(1) and assign a value to it. I tried 1, TRUE, true, but none of them worked. There is a bug in MySQL returning bit values as PHP chr(0) and chr(1) instead of booleans / integers. They are evaluated true, regardles of value. Setting works as expected - setting to 1 works fine. So it is possible, that the value is set correctly, it's just not displayed. In my application using BIT fields I have hack in my MySQL driver converting their values to booleans, here's the code:
/**
* Fixes a result row
* @param mixed $row
* @return mixed
*/
protected function fix_result($row) {
if ($row) foreach ($row as &$v) {
if (is_string($v)) {
if (preg_match(
elseif (strlen($v) == 1 && ($c = ord($v[0]))<2) $v = (bool) $c;
else $v = rtrim($v, chr(0)); // for char data type
}
}
return $row;
}
The code is a method of mysqli_result class extension. The same I use in mysqli_stmt extension class. The last check is very important for comparing CHAR data - MySQL frontends sometimes pads CHAR values with chr(0), sometimes not. It caused severe and hard to find bugs in application. First chr(0) should be considered as end of data and not included in result. Chr(0) should be returned unchanged only for binary types.
Related branches
Changed in chive: | |
status: | Confirmed → In Progress |
Changed in chive: | |
milestone: | 0.2.1 → 0.3.0 |
Changed in chive: | |
status: | In Progress → Fix Committed |
Changed in chive: | |
status: | Fix Committed → Fix Released |
Thanks for the bug-report.
This will be fixed in 0.2.1