PMA\libraries\Util::printableBitValue PHP Method

printableBitValue() public static method

Converts a bit value to printable format; in MySQL a BIT field can be from 1 to 64 bits so we need this function because in PHP, decbin() supports only 32 bits on 32-bit servers
public static printableBitValue ( integer $value, integer $length ) : string
$value integer coming from a BIT field
$length integer length
return string the printable value
    public static function printableBitValue($value, $length)
    {
        // if running on a 64-bit server or the length is safe for decbin()
        if (PHP_INT_SIZE == 8 || $length < 33) {
            $printable = decbin($value);
        } else {
            // FIXME: does not work for the leftmost bit of a 64-bit value
            $i = 0;
            $printable = '';
            while ($value >= pow(2, $i)) {
                ++$i;
            }
            if ($i != 0) {
                --$i;
            }
            while ($i >= 0) {
                if ($value - pow(2, $i) < 0) {
                    $printable = '0' . $printable;
                } else {
                    $printable = '1' . $printable;
                    $value = $value - pow(2, $i);
                }
                --$i;
            }
            $printable = strrev($printable);
        }
        $printable = str_pad($printable, $length, '0', STR_PAD_LEFT);
        return $printable;
    }

Usage Example

 /**
  * Get data row action
  *
  * @return void
  */
 public function getDataRowAction()
 {
     $extra_data = array();
     $row_info_query = 'SELECT * FROM `' . $_REQUEST['db'] . '`.`' . $_REQUEST['table'] . '` WHERE ' . $_REQUEST['where_clause'];
     $result = $this->dbi->query($row_info_query . ";", null, DatabaseInterface::QUERY_STORE);
     $fields_meta = $this->dbi->getFieldsMeta($result);
     while ($row = $this->dbi->fetchAssoc($result)) {
         // for bit fields we need to convert them to printable form
         $i = 0;
         foreach ($row as $col => $val) {
             if ($fields_meta[$i]->type == 'bit') {
                 $row[$col] = Util::printableBitValue($val, $fields_meta[$i]->length);
             }
             $i++;
         }
         $extra_data['row_info'] = $row;
     }
     $this->response->addJSON($extra_data);
 }
All Usage Examples Of PMA\libraries\Util::printableBitValue
Util