james david low

live / work / play / worship


Ok, if you subscribe to my blog for personal reasons, this isn’t very relevant to you, but for those of you here for the programming, on a recent project I had some problems getting the BIT field in MySQL to be interpreted as a boolean in PHP. There are a couple of discussions on the net about it here and here, but I couldn’t find any simply stated solutions on the net. So here’s what I ended up doing. You can also download the file here, rename the file .php to use it.

function mysql_bit($bit) {
return ord($bit) == 1;

If you found any of the software useful, please consider supporting its further developement by donating.


    ahahah i already got this via your feed… but yeah very cool. you could of course use a proper programming language like java or .net, and be done with all these pedantic issues… :P ehehehe

    glad to be tagged though :)

    12:00am / Aug 14th / 08 Janakan Arulkumarasan (via Facebook)

    PHP is great for somethings though, its fast to write websites in, and anything that processes text. Regex FTW !!!

    12:00am / Aug 16th / 08 James Low (via Facebook)

    A boolean is a variable that evaluates as true or false. A BIT in MySQL is a number that is only 1 bit long. a 32bit number goes as high as 4,294,967,295, but a 1 bit number only goes as high as 1. So it has 2 values 0 and 1, which is almost a boolean 0 = false, 1 = true.

    Unfortunately when PHP reads a BIT from MySQL into PHP variable, it seems to treat it as a ASCII string character that is represented by the number 0 or 1. In ASCII the letter A has the value 65, B 66, etc.

    The ord() function in MySQL will convert from a number to the ASCII equivilant. For example ord(65) = A, etc. So I used the ord function to convert the number 0 or 1 to the ASCII equivilant and then compare with the BIT returned from MySQL.

    12:00am / Aug 16th / 08 James Low (via Facebook)

    o.o me is confuzzled. haha! I think I know what a boolean is… >.<

    11:15pm / Aug 16th / 08 David James Shurr (via Facebook)

    Ahhh, cool. o.O MySQL and PHP are different languages, right?

    1:00am / Aug 18th / 08 David James Shurr (via Facebook)

    Yea, MySQL is a language for create and querying databases. PHP is a scipting language similar to python.

    6:51am / Aug 18th / 08 James Low (via Facebook)

    Oooh, cool.

    2:20pm / Aug 18th / 08 David James Shurr (via Facebook)

    PHP is pretty much a clone of Perl. You’d probably offend a number of Python yahoo’s by saying PHP is like Python.

    I could go into specifics, like Python uses Optimized C in it’s cold and strict heart, while perl, is just a hacker language.

    By the way, .net has Regex. Most modern languages do.

    3:51am / Feb 19th / 09 Slackshot

    thanks, I forgot about the ord function.

    9:58am / Apr 15th / 09 loudacris

    Thanx mate…… :D

    5:28am / May 14th / 09 Gareth Evans

    Try this instead:

    function mysql_bit($bit) {
    if(ord($bit) == 1)
    return 1;
    return 0;

    MySQL bit = 0/false…this returns 0, 1/true returns 1.

    Happy Days.

    5:38am / May 14th / 09 Gareth Evans

    Nice one for posting this.

    Although I do like those cute ascii representations, a functioning class is more useful to me innit.


    12:21am / Nov 4th / 09 Ralph

    This was very helpful. Thank you for sharing!

    6:34am / Mar 15th / 10 Marty Y. Chang

    Thanks for this tip, really made my day

    3:22pm / Sep 29th / 10 Mohamed Faramawi

    Thanks for the tip! Just wait till php 9 comes out! LOL

    6:33pm / Nov 10th / 10 Jonathan Ranes

    Tons of thanks for the tip. very helpful

    1:24am / Dec 16th / 10 Kay

    […] is a really random post, but like the MySQL Bit post, it took me ages to find the right information on the internet, so thought I would blog it. I […]

    9:20am / Dec 17th / 10 james david low / work