Unable to get/set BIT value

Bug #523713 reported by Adam Łyskawa
8
This bug affects 1 person
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('/^\d+\.\d+$/', $v)) $v = (string) float($v);
        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

Revision history for this message
David Roth (davrot) wrote :

Thanks for the bug-report.
This will be fixed in 0.2.1

Changed in chive:
assignee: nobody → David Roth (davrot)
importance: Undecided → High
milestone: none → 0.2.1
status: New → Confirmed
David Roth (davrot)
Changed in chive:
status: Confirmed → In Progress
Changed in chive:
milestone: 0.2.1 → 0.3.0
David Roth (davrot)
Changed in chive:
status: In Progress → Fix Committed
Changed in chive:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.