Last Updated: February 25, 2016
·
2.609K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside