use MySQL's but as true || false
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: use MySQL's but as true || false

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    267

    use MySQL's but as true || false

    Hi,

    I have a field called is_closed in MySQL table. It's a bit datatype which means it's 0 or 1

    I want to know how to deal with it in PHP, like how can I (IF) it?

    and is there any way to deal with it as if (is_closed == true)?


    Thanks
    Jassim

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,845
    What have you tried?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    You can use var_dump() to get more information about a variable.

  4. #4
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    Hi,

    I tried:

    PHP Code:
    $is_closed $mysql_row["is_closed"]; // with and without (bool); 
    then I tried both:

    PHP Code:
    if ($is_closed === 1// with and without (bool); 
    and

    PHP Code:
    if ($is_closed === 1// with and without (bool);\ 

    but with all above, IF is failing to understand the condition.


    Thanks,
    Jassim

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,687
    Quote Originally Posted by jrahma View Post
    Hi,

    I tried:

    PHP Code:
    $is_closed $mysql_row["is_closed"]; // with and without (bool); 
    then I tried both:

    PHP Code:
    if ($is_closed === 1// with and without (bool); 
    and

    PHP Code:
    if ($is_closed === 1// with and without (bool);\ 

    but with all above, IF is failing to understand the condition.


    Thanks,
    Jassim
    But did you try:
    PHP Code:
    var_dump($mysql_row);
    var_dump($is_closed); 
    as Brad suggested?
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    yes my friend

    it's returning the correct value.

    here is the details:

    this is an inoute hidden value and I set it to 1

    HTML Code:
    <input type="hidden" id="chkSupportReply" name="chkSupportReply" value="1">
    then reading it here:

    PHP Code:
    $is_support_reply $_POST['chkSupportReply']; 
    the checking if the value is true then send an email like this:

    PHP Code:
    if ($is_support_reply === 1
    the var_dump output is here:

    string(1) "1"

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    It's returning the correct value, yes. However, the problem is you're testing for an incorrect value. "1" and 1 are not the same thing; the former is a string containing the character "1" (in ASCII, hex value 0x34). You're comparing that with the number one, hex value 0x01. Since the number 0x34 does not equal the number 0x01, your if() statement is correctly evaluating to false.

    Either test for the correct value, or use the "equal" operator and not the "identical" operator. See operators.comparison for more information about PHP's comparison operators.

    EDIT: Furthermore, I'm confused why your first post talks about MySQL, yet the code you've shown us above has nothing to do with MySQL and everything to do with external POST'ed data from an HTML form.
    Last edited by bradgrafelman; 11-08-2013 at 01:53 PM.

  8. #8
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,845
    And note that MySQL's BIT-pattern type is not the same thing as MySQL's excuse for a BOOLEAN type; one is a string type and the other is an alias for TINYINT(1).

    And yes, as bradgrafelman pointed out, POSTed form data is not the same thing as a database result set.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  9. #9
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    EDIT: Furthermore, I'm confused why your first post talks about MySQL, yet the code you've shown us above has nothing to do with MySQL and everything to do with external POST'ed data from an HTML form.
    because I have the same problem with $is_closed which is a bit field in MySQL and I trying the same way to read it.

    so to check if true when posting it from <input> field I should use:

    PHP Code:
    if ($is_support_reply === "1"
    what about the value from a bit field in MySQL? is it also the same. Can I use it like this:

    PHP Code:
    $is_closed $mysql_row["is_closed"];
    if (
    $is_closed === "1"

  10. #10
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Quote Originally Posted by jrahma View Post
    because I have the same problem with $is_closed which is a bit field in MySQL and I trying the same way to read it.
    How do you know it's the same? Retrieving values from a database is most definitely not the same as parsing POST'ed data from a form, so trying to handle both in the same manner could very well be a problem.

    Quote Originally Posted by jrahma View Post
    what about the value from a bit field in MySQL? is it also the same.
    Maybe. Maybe not. You'll have to examine what value you get back to find out.

  11. #11
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    thanks bradgrafelman . One more question please.

    Isn't it possible to say $is_support_reply = true? I mean by global setting or php configuration or anything else?

  12. #12
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    You could define the variable in a PHP script that gets automatically included before every file; see the auto_prepend_file directive.

  13. #13
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    Hi,

    I tried the $is_support_reply as you mentioned with "1" and it's working perfectly but I am still having a problem when it comes from a bit field from MySQL. I tried all the following but non of it worked.

    please help...

    PHP Code:
    if ((bool)$mysql_row["is_support_reply"] == 1)
    if ((bool)
    $mysql_row["is_support_reply"] == "1")
    if (
    $mysql_row["is_support_reply"] == 1)
    if (
    $mysql_row["is_support_reply"] == "1"

  14. #14
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    Again, use var_dump() to determine the exact value of a variable.

  15. #15
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    var_dump result is:

    string(1)

    but still not getting what I want. Here is what I want:

    PHP Code:
    var_dump($mysql_row["is_support_reply"]);
    if (
    $mysql_row["is_support_reply"] == "1")
    {
        echo 
    "<div style='background-color: #eee9e9; padding: 10px;'>";
        echo 
    "<b>Support Team Replies :</b><br><br>";
    }
    echo 
    date('d M Y - H:i'strtotime($mysql_row['created_date']));
    echo 
    "<br><br><hr>";
    echo 
    $mysql_row["support_ticket_message"];
    echo 
    "<br><br>";
    if (
    $mysql_row["is_support_reply"] == "1") echo "</div>";
    echo 
    "<br>"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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