Bit(1) datatype from mySQL

Bit(1) datatype from mySQL

am 07.01.2006 15:45:42 von Larry Bradley

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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Bit(1) datatype from mySQL

am 07.01.2006 17:02:01 von Bastien Koert

You can cast it to a boolean
(http://phpbuilder.com/manual/en/language.types.type-jugglin g.php#language.types.typecasting)

if (bool($IsAMember) == true) ...


bastien


>From: Larry Bradley
>To: php-db@lists.php.net
>Subject: [PHP-DB] Bit(1) datatype from mySQL
>Date: Sat, 07 Jan 2006 09:45:42 -0500
>
>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
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Bit(1) datatype from mySQL

am 07.01.2006 19:20:38 von Micah Stevens

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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Bit(1) datatype from mySQL

am 08.01.2006 15:35:22 von El Bekko

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

Try using a set() field with values true and false

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php