blueworld Posted September 6, 2006 Share Posted September 6, 2006 I'm trying to store some data to a bitfield column in MySQL 5, and then retrieve it back to an integer in PHP. Some part of the process isn't working, and I've been unable to find any helpful information in my searches.Here's my test code:[code]$query1 = "UPDATE phpbb_users SET warning1 = b'0101' WHERE user_id = 2";mysql_query($query1);$query = "SELECT warning1 FROM phpbb_users WHERE user_id = 2";$result = mysql_query($query);$row = mysql_fetch_assoc($result);$bitfield = $row['warning1'];echo gettype($bitfield);echo '<br>' . $bitfield;echo '<br>' . (int) $bitfield;[/code]The column warning1 is of type BIT(32). Output of this script is [code]string0[/code]Does anybody know why I'm getting a string, and how I can get an integer? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 6, 2006 Share Posted September 6, 2006 How is the table defined? Quote Link to comment Share on other sites More sharing options...
blueworld Posted September 6, 2006 Author Share Posted September 6, 2006 The relevent part of the definition is:ALTER TABLE phpbb_users ADD ( warning1 BIT(32) ) Quote Link to comment Share on other sites More sharing options...
Barand Posted September 6, 2006 Share Posted September 6, 2006 instead of the gettype() call, tryecho mysql_field_type($result, 0); // --> intI'm guessing that db fields are retrieved as string representations of the contents Quote Link to comment Share on other sites More sharing options...
blueworld Posted September 6, 2006 Author Share Posted September 6, 2006 Thanks! [code]echo mysql_field_type($result, 0);[/code] returns "unknown" for me. It doesn't seem to be a string representation.I'm really puzzled by this. At this point it seems like it would be easier to just use integer fields instead of bit fields. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 6, 2006 Share Posted September 6, 2006 I think BIT is a synonym for TINYINT (single byte) Quote Link to comment Share on other sites More sharing options...
Barand Posted September 6, 2006 Share Posted September 6, 2006 if you tryvar_dump($row['warning']);you'll see it's a string in PHP - as I suspected - string representations are returned Quote Link to comment Share on other sites More sharing options...
blueworld Posted September 7, 2006 Author Share Posted September 7, 2006 According to the MySQL manual,[quote]BIT[(M)]A bit-field type. M indicates the number of bits per value, from 1 to 64. The default is 1 if M is omitted.This data type was added in MySQL 5.0.3 for MyISAM, and extended in 5.0.5 to MEMORY, InnoDB, and BDB. Before 5.0.3, BIT is a synonym for TINYINT(1). [/quote]You're right, it was being returned as a string. The binary data was being interpreted as ascii encoding, so I got 4 characters for my 32-bit field. I guess I have to write a function now to convert the characters into an appropriate integer.Thanks for your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.