php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70170 mysql bit column always fetches as empty string
Submitted: 2015-07-30 18:50 UTC Modified: 2015-07-30 21:18 UTC
From: pletnev dot rusalex at gmail dot com Assigned:
Status: Not a bug Package: PDO MySQL
PHP Version: 5.6.11 OS: debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pletnev dot rusalex at gmail dot com
New email:
PHP Version: OS:

 

 [2015-07-30 18:50 UTC] pletnev dot rusalex at gmail dot com
Description:
------------
mysql bit column when there are values b'0' and b'1' fetched by PDO as empty strings ""

Test script:
---------------
<?php
/**
DROP TABLE IF EXISTS `bit_values` CASCADE;

CREATE TABLE `bit_values` (
  `id`      INT(11) NOT NULL AUTO_INCREMENT,
  `val` bit(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `bit_values` (id, val) VALUES (1, b'0'), (2, b'1');
 */
$pdo = new PDO(
    'mysql:host=localhost;dbname=dbname',
    '',
    ''
);
$sth = $pdo->prepare("SELECT * FROM bit_values");
$sth->execute();
$r = $sth->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>'; var_dump($r); echo '</pre>'; die();



Expected result:
----------------
 array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["val"]=>
    string(1) "0"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["val"]=>
    string(1) "1"
  }
}

Actual result:
--------------
 array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["val"]=>
    string(1) ""
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["val"]=>
    string(1) ""
  }
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-30 19:00 UTC] pletnev dot rusalex at gmail dot com
$link = mysql_connect('localhost', '', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('yiitest', $link) or die('Could not select database.');
$res = mysql_query("SELECT id, val FROM bit_values", $link );
while($row = mysql_fetch_assoc($res)) { 
	var_dump($row);
} 
mysql_close($link);
 [2015-07-30 20:22 UTC] cmb@php.net
Related to bug #70132.
 [2015-07-30 21:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-07-30 21:18 UTC] requinix@php.net
As the output shows,
> string(1) ""
the string is not actually empty: it contains \x00 or \x01, which isn't printable.

@cmb linked another bug which is about changing that value to something better.
 [2015-07-31 04:43 UTC] pletnev dot rusalex at gmail dot com
So ok. It's not printable. But why mysqli then cast it into strings "0" and "1" ? 

If you use mysqli functions in a given script, the result will be "0" and "1" respectively. 

Suppose there must be something conventional behavior. Or it must be documented.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 08:02:42 2024 UTC