Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL bit value in PHP doesn't work

Tags:

php

mysql

yii

I have a bit type value hasFreePackage in database which stores 1 or 0.

Here is my code below, This is working on my localhost and but whenever i try to upload on live server this code doesn't work.

public function checkBitValue($hasFreePackage)
{
   $hasFreePackage= ($hasFreePackage== 0x01)
   if($hasFreePackage==1)
       return 1;
   else
       return 0;
}

As live server is linux platform and i'm running windows on local. my code works on windows but not on linux So i changed my code to this code..

public function checkBitValue($hasFreePackage)
{
   $FreePackage= ($hasFreePackage== 0x01)
   if($FreePackage==1)
       return 1;
   elseif(ord($FreePackage==1))
       return 1;
   else
       return 0;
}

but still not working. I'm using yii 1.1 php framework and php versions are different.At linux is 5.2.0 and at local (windows) is 5.7


1 Answers

Even simpler:

{
    return $hasFreePackage ? 1 : 0;
}

Or, more clearly:

{
    return $hasFreePackage ? TRUE : FALSE;
}

The rationale: 0 is FALSE; non-zero numbers are TRUE.

Still better: Keep that in mind and don't bother checking true/false value against true/false!

Caution: NULL and '' muddy up logic like this. Hence the "rationale" spoke only of numbers.

like image 52
Rick James Avatar answered Jul 15 '26 08:07

Rick James