Jump to content

Help with if / else with a MySQL bit field?


dazedconfused
Go to solution Solved by Psycho,

Recommended Posts

Trying to do an if / else with a bit field.  My background is Classic ASP so please bear with me as I'm new to PHP and am trying to convert!

 

Here's what I've been trying:

 

<?php

$dimmer=$row_rsPRODFULL['LEDdimmable']

if ($dimmer=b'1')

{

echo "Yes";

}

else

{

echo "no";

}

?>

 

I've also tried:

if ($dimmer="true")

if ($dimmer="1")

 

...nothing works!  :(

Hope someone can help.

 

PS.  Why can't I paste into this text box, even using the Copy/Paste tools above?  Using IE11.

 

Thank you

NJ

 

Link to comment
Share on other sites

 

 

Should they both be changed to the comparison operator?

Only the one that is testing for equality should be "=="

 

To test if a particular bit is set you need to use a logical "and" (&).

 

EG

 

Suppose your variable contains 00000101

// 00000101
//      ^ to check if this bit is set use logical "and" with 4 (binary value of the bit)

if ($myvar & 4) {
    // bit is set so do something
}

PS My name is Barand, not sen. Funnily enough, my name in my posts appears in exactly the same place as your name does in your posts. Strange but true.

Link to comment
Share on other sites

Sorry for the confusion with the name.  I mentioned that I was new to PHP but I should also have mentioned that I'm completely new here.  My apologies for any offence caused.

 

My variable contains either a 1 or 0 value.  It is a BIT field.  In Access or MSSQL this would be a checkbox Yes/No or True/False value.

 

I'm simply looking to display one item of text if the bit field from the MySQL table is true and another if it is false.  Considering how easy this is in Classic ASP:

<%IF <databasefield> = "1" Then%>This is true<%ELSE%>This is False<%End IF%>

 

...I'm struggling to comprehend why this is so difficult in PHP.

 

I have tried your suggestion...

 

<?php

if ($row_rsPRODFULL['LEDdimmable'] & 1)

{

echo "Yes";

}

else

{

echo "No";

}

?>

 

...but it only displays the word "No" even though a field is true.

Hoping someone can help me sort this out.

Is PHP really this complicated?

 

Thank you
NJ

Link to comment
Share on other sites

I've added this within the repeat region on my page, which is where I'm trying to get this if/else, yes/no, to appear:

 

<?php

var_dump($row_rsPRODFULL['LEDdimmable']);

?>

 

...which is what I assume you wanted me to do (sorry if I've assumed incorrectly).

This is what is displayed:

 

string(1) ""

 

I have no idea why it's delivering that.  If I ask it to just display the recordset field value:

<?php echo $row_rsPRODFULL['LEDdimmable']; ?>

 

...it is blank.

Hope someone can help.  Tearing my hair out with this now.  :

 

Thank you
NJ

Link to comment
Share on other sites

I have the field set as BIT with a default value of 0.

 

I assumed, I now fear incorrectly, that the values for the bit field should either be 1 or 0 - to indicate either true or false.  I enter 1 where I want the value to be true, and leave it as 0 when I want it to be false.

 

What should the values be for a bit field to signify either true or false?

 

Really appreciate the help.  Thank you.
NJ

Link to comment
Share on other sites

FYI: The term you wanted to use for a True/False value is a BOOLEAN - not a "bit field".

 

Also, if you have a condition to check if a value is True/False you don't need to have the comparison operator. You can just do something such as this:

 

if ($row_rsPRODFULL['LEDdimmable'])   {
    //Do this if value is true
}   else   {
    //Do this is value is false
}

 

Also a couple other notes. PHP is a weak type language (as opposed to a strict type language). One of the properties of that is that when comparing values they don't have to be the same "type". For example, the boolean TRUE and the number 1 are both interpreted as true. So, if(TRUE == 1) would result in a true result. This is important because you can't store an actual boolean in the database. You would store a 1 or 0. But, you need to keep this in mind, because sometimes you want to compare to value in a strict sense. For these you would use the string comparison operators. E.g. if(TRUE === 1) - note the THREE equal signs. That comparison would result in a false result.

Link to comment
Share on other sites

  • Solution

I have the field set as BIT with a default value of 0.

 

I assumed, I now fear incorrectly, that the values for the bit field should either be 1 or 0 - to indicate either true or false.  I enter 1 where I want the value to be true, and leave it as 0 when I want it to be false.

 

What should the values be for a bit field to signify either true or false?

 

As I stated in my last response, this is not a "bit" field. You really want a boolean - but there is no true boolean in MySQL (that I am aware of). You would instead want to use a Tiny INT field and store a 1 (true) or 0 (false).

 

A bit field is one used to store a bit-wise piece of data - i.e. base 2 notation such as 11010011

Link to comment
Share on other sites

It's not a huge database but I was using BIT in order to keep its size down.  It would appear that I have completely misunderstood "BIT" in both MSSQL and MySQL, from what you are saying.  Jeezo!

 

Would the simplest solution be to just use TINY INT then, with 1's for true and 0's for false?

I guess that would also give me the flexibility to have a third value - 3 for example (for 'maybe')?

 

Thank you Psycho.
NJ

Link to comment
Share on other sites

It's not a huge database but I was using BIT in order to keep its size down.  It would appear that I have completely misunderstood "BIT" in both MSSQL and MySQL, from what you are saying.  Jeezo!

Not entirely. SQL Server's BIT type does act as a boolean field and stores only a single 1 or 0 (or NULL if allowed).

 

Mysql's BIT type allows for multiple bits to be stored, but could still be made to essentially be a boolean field by using a BIT(1) definition (allow only 1 bit).

 

I've never used mysql's BIT type so i'm not sure how it would interact with PHP when selecting the value back out. You could use the HEX() function to find out what is returned.

 

Would the simplest solution be to just use TINY INT then, with 1's for true and 0's for false?

TINYINT is commonly used for boolean fields, partly because before mysql5 it was the best option as there was no BIT type. As you mentioned, with TINYINT you could add a few additional values as well such as a value for 'maybe'. Another common option is to use a CHAR(1) field and strings like 'Y' or 'N'. Both would use the same amount of storage space, it mostly just comes down to what your preference is.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.