Re: Bit(1) datatype from mySQL
| From: | Micah Stevens | Date: | Sat, 07 Jan 2006 18:20:38 +0000 |
| Subject: | Re: Bit(1) datatype from mySQL | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-41003@lists.php.net to get a copy of this message | ||
Use a tinyint field in mysql, (which is what bool gets translated to I think)
and you'll get a number back, 0 = false, 1 = true, and you can:
if ($fieldname) {
do true item;
}
if (!$fieldname) {
do false item;
}
This works for me, I've done before quite a bit in the past.
-Micah
On Saturday 07 January 2006 6:45 am, Larry Bradley wrote:
> Greetings:
>
> I'm new to PHP, although I've been a programmer all my life. I had been
> doing a bit of web database work with VB Script and ASP and mySQL, and then
> decided to try PHP. I've run into a couple of things that bother me.
>
> I have boolean fields (i.e. bit(1)) in the mySQL database. When I retrieve
> data from the mySQL server into an array or an object, it seems as if all
> the data fields are returned as characters. For most data types this does
> not matter, since PHP will handle them properly. This an integer 46 being
> retrieved as a character '46' is OK.
>
> However, the boolean is a curse, as it comes back as a character containing
> hex 00 or hex 01, and I have to use a test such as:
>
> if (ord(IsAMember) == 0) ...
>
> I can't just treat it as a boolean.
>
> I have got around the problem by using a construct such as:
>
> select if(IsAMember,1,0) as IsAMember
>
> in the SQL select, but this is a bit of a pain.
>
> Any thoughts?
>
> Thanks
>
> Larry Bradley
> Orleans (Ottawa), Ontario, CANADA