Processing BIT data "\x0\x0\x0\x0\x0\x0\x0\x5"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gmckay
Forum Newbie
Posts: 11
Joined: Mon Apr 28, 2008 5:13 pm
Location: Melbourne, Australia

Processing BIT data "\x0\x0\x0\x0\x0\x0\x0\x5"

Post by gmckay »

I have setup a BIT(64) field in MYSQL.
I can populate this field OK converting the flags "0000000000000000000000000000000000000000000000000000000000000101", but when the data is returned in a query it is represented as "\x0\x0\x0\x0\x0\x0\x0\x5" and I can't find any hints online how to convert this back to flags....

I've pulled out what little hair I had left, threw my arms in the air and post this plea for HELP!!

I know you are going to say 'why?', but I'm not looking for criticism, just an answer...

p.s. I'm a mainframe COBOL programmer and all this php stuff is a bit foreign...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Processing BIT data "\x0\x0\x0\x0\x0\x0\x0\x5"

Post by Weirdan »

[sql] SELECT bin(fieldName) FROM tableName ... [/sql]
gmckay
Forum Newbie
Posts: 11
Joined: Mon Apr 28, 2008 5:13 pm
Location: Melbourne, Australia

Re: Processing BIT data "\x0\x0\x0\x0\x0\x0\x0\x5"

Post by gmckay »

Thanks, I'll have a go at that, but as that will require me to change all the SELECT statements from "SELECT *" to "SELECT bin(fieldName), fieldName2, ...." I was really looking for a post-select coding solution.
gmckay
Forum Newbie
Posts: 11
Joined: Mon Apr 28, 2008 5:13 pm
Location: Melbourne, Australia

Re: Processing BIT data "\x0\x0\x0\x0\x0\x0\x0\x5"

Post by gmckay »

Coding "SELECT *, bin(fieldName) as flags .." worked well. I'll just propagate that wherever needed.
Thanks muchly...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Processing BIT data "\x0\x0\x0\x0\x0\x0\x0\x5"

Post by Weirdan »

gmckay wrote:I was really looking for a post-select coding solution.
That would really complicate things, especially if you use 32bit php (because you're using 64bit bit field). Anyway, just in case you'd ever need it: you would need to unpack data to two unsigned longs (note that byte order is machine-dependent) and then either convert them to bit string using decbin or use them as ints and perform bit arithmetics on them yourself.
Post Reply