ISSUE :
 
Profile photo
Submitted by SBajra
on April 3rd, 2010 at 21:17:10 EST

MYSQL Bit field with PHP

I wrote my program in windows environment with PHP - MYSQL. Everything works fine, I am able to compare my retrieved bit field with numeric number. But once I moved my code to Production environment which is Linux server, this code stopped worked. I am getting different value for bit value stored from production MYSQL.

Before I was able to do

$Data = Data from database
if ($Data["isOKToEmail"]==1)
{
//do something
}


But this code does not work in Linux version of MYSQL - PHP.

This is kind of urgent. I highly appreciate for any help.


Profile photo
Replied by RaKumar
on April 3rd, 2010 at 21:45:49 EST
use php built in function: ord() . This should work for both Windows and Linux dealing with MySQL BIT field using PHP.

For example:

$Data = Data from database
if (ord($Data["isOKToEmail"])==1)
{
//do something
}
Accepted Answer
Accepted
 Answer

Profile photo
Replied by SBajra
on April 4th, 2010 at 11:51:07 EST
Thank you RaKumar. This resolves my issue. Highly appreciate it.

Profile photo
Replied by SBajra
on April 8th, 2010 at 07:58:30 EST
For more robust solution, I end up doing something like this:

$Data = Data from database
if ($Data["isOKToEmail"]==1 || ord($Data["isOKToEmail"])==1)
{
//do something
}


And this works perfectly in both windows and linux version using php.

Profile photo
Replied by David Wu
on June 11th, 2011 at 19:31:23 EST
This issue is because Windows return back digit 0 or 1 for MySql boolean value whereas Linux returns back 0 binary bit or 1 binary bit.

In Windows digit 0 is ASCII code 48 (110000 in binary bit) and digit 0 is ASCII code 49 (110001 in binary bit)

This is the reason Linux binary result is not equal to Windows binary result.

And thats why the sample code provided by SBaj works for both.

$Data["isOKToEmail"]==1 -> this will work for Windows
ord($Data["isOKToEmail"])==1) -> this will work for Linux

So, if you use both, it will work for both.