Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2003

    Unanswered: dealing with MySQL's bit n PHP


    I have a bit field in MySQL called is_closed and it has either 0 for false or 1 for true.

    I want to know how can I deal with it in PHP? How can I IF it and say:

    if (mysql_row["is_closed"] == 1)

    Do I have to say == 1 or == "1"? do I have to say (bool)mysql_row["is_closed"]?

    Please guide as I tried different ways but still not getting it working!


  2. #2
    Join Date
    Jan 2007
    Provided Answers: 10
    Updated answer:

    if (mysql_row["is_closed"]) { ... }
    if (! mysql_row["is_closed"]) { ... }
    // or
    if (mysql_row["is_closed"] == chr(0x01)) { ... }
    // false
    if (mysql_row["is_closed"] == chr(0x00)) { ... }
    Alternatively modify your MySQL to return a different data type altogether e.g.
    SELECT Cast(is_closed As unsigned integer) ...
    SELECT Cast(is_closed As char(1)) ...
    SELECT CASE WHEN is_closed THEN 'True' ELSE 'False' END ...
    Last edited by gvee; 11-11-13 at 09:11.
    Home | Blog

  3. #3
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    can you provide the DDL that created the table
    do you really mean you created a column of type bit to store boolean values?

    Although the promised boolean data types doesn't seem to be implemented as yet, I would expect the best way of faking boolean is to use some form of integer
    then I'd set a a true false value from PHP, then you know how those values are represented and use that representation in your PHP code

    if you set up your table using the synonym of bool the MySQL engine has probably created a column with a datatype of tinyint, nullable. it looks like MySQL treats 0 as false, 1 as true (Which gives you potentially 3 states true, false and NULL)
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Apr 2003
    this is the DDL followed by additional comment:

    CREATE TABLE `support_ticket_messages` (
    `support_ticket_message_id` int(11) NOT NULL AUTO_INCREMENT,
    `support_ticket_message_guid` varchar(255) DEFAULT NULL,
    `support_ticket_id` int(11) DEFAULT NULL,
    `is_support_reply` bit(1) DEFAULT b'0',
    `support_ticket_message` text,
    `created_date` datetime DEFAULT NULL,
    PRIMARY KEY (`support_ticket_message_id`)

    Have a look at this page:

    unable to sign up[^]

    you'll see the value returned is string(1) "" but it's actually a bit value from MySQL and I am using this to read it:

    SELECT * FROM support_ticket_messages WHERE support_ticket_id = param_ticket_id ORDER BY support_ticket_message_id DESC;

    I am getting the correct value when running it on my localhost and I am also getting the correct value when running the stored procedure on the database.

    Then why the PHP is not showing it when I try:

    if ($is_support_reply)
    echo "<div style='background-color: #eee9e9; padding: 10px;'>";
    echo "XoomPage Replies :<br><br>";


    if ($mysql_row["is_support_reply"])
    echo "<div style='background-color: #eee9e9; padding: 10px;'>";
    echo "XoomPage Replies :<br><br>";

    Please help..


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts