weird bit character display
I am storing x and y as bits in mysql database, upon fetch the record using php fetch_assoc, the display is gobbled. And I can't 开发者_StackOverflowfind a way to test the value to see if it's true or false... I tried using if($val), if($val==true) if($val===true), if $val is false, all 3 tests still returns true. any idea? [x] => � [y] =>
thanks for the answer below, I now use:
function mysql_bit($bit) { if(ord($bit) == 0 || ord($bit) == 48)return false; return true; }
to handle both mysql bit value and regular string/int value(from http request for example).
Does this post help?
- http://jameslow.com/2008/08/12/mysql-bit-in-php/
I had this issue a looooong time ago... it is read as an ascii '1' or '0'. I don't think my solution at the time was exactly as nice, but I don't have the source anymore so I will pretend it was!
function mysql_bit($bit) {
return ord($bit) == 1 || $bit == 1;
}
Had the same problem, searched SO, and came across this post...
Which, in turn, led me to this section in the mysql manual.
You need to do something like this when setting bit values:
update `t` set `bitfield`=b'0' where ...
That tells mysql that '0' is the bit value rather than the character 0.
Likewise, reading it gives binary data rather than the characters 0 or 1, but you can typecast it:
select cast(`bitfield` as unsigned) from `t` where ...
Or we can all just go back to using tinyint(1)
...
精彩评论