Last Updated: February 25, 2016
·
2.368K
· arthurbarros

Casting MySQL bit type at PHP runtime

At some point in your project that you think represent boolean in your data structure as a mysql bit was a good idea, yet you do not find out because after a query to the database through PHP your bit does not behave like a boolean as you were expecting.

There are three ways you can do with a bit mysql behaves as a boolean:

At runtime in php:

$bitValue = ($bitValue == 0x01);

or

ord($bitvalue)

or in your query:

SELECT CAST (AS bitfield unsigned int) FROM ...

I hope it helps you.