MySQL select b'1 'returns integer 1 or ASCII character 1 depending on environment

On my home computer

mysql_fetch_row( mysql_query(" select b'1' ") )[0]

returns the string "1".

But when placed on a web server, a string with the ASCII character 1 is returned.

Doc says -

Bit values ​​are returned as binary values. To display them in printable form, add 0 or use a conversion function such as BIN ().

But on my local machine, it still returns β€œ1” without any conversion made by me.

How can I have the same behavior on my web server?

If I get the same behavior, I don't need to convert my PHP codes with

 $row = mysql_fetch_row( mysql_query(" select bit1_field from .. where .. ") ); if( $row[0] === '1' ) ...; 

to

 ... select bit1_field+0 as bit1_field ... 

where bit1_field is of type bit(1) .

+8
php mysql
source share
1 answer

It seems you are using two different drivers on the machines. There are two, php5-mysqlnd and php5-mysql . The site factor wrote about the behavior for the BIT field at the end of April, and I also have several machines with the same version, but with different disks. Probably due to the fact that the driver does not change when upgrading from an earlier version, but when php> 5.4 is installed, it is installed by default with php5-mysqlnd . Here is the MySQL page on differences .

+2
source share

All Articles