Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - <?php
 - /*
 - --
 - -- Table structure for table `debug`
 - --
 - CREATE TABLE IF NOT EXISTS `debugbits` (
 - `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
 - `text` varchar(64) NOT NULL,
 - `mybit` bit(8) NOT NULL,
 - PRIMARY KEY (`id`)
 - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
 - --
 - -- Dumping data for table `debug`
 - --
 - INSERT INTO `debugbits` (`id`, `text`, `mybit`) VALUES
 - (1, 'one', b '00000001'),
 - (2, 'two', b '00000010'),
 - (3, 'three', b '00000100'),
 - (4, 'four', b '00001000'),
 - (5, 'five', b '00010000'),
 - (6, 'six', b '00100000'),
 - (7, 'seven', b '01000000'),
 - (8, 'eight', b '10000000'),
 - (9, 'one and two', b '00000011'),
 - (10, 'one and three', b '00000101'),
 - (11, 'one and four', b '00001001'),
 - (12, 'one and five', b '00010001'),
 - (13, 'one and six', b '00100001'),
 - (14, 'one and seven', b '01000001'),
 - (15, 'one and eight', b '10000001');
 - */
 - /*
 - This function converts the bits to a more readable string of ones and zeros,
 - like 00000001 (note that the first bit is on the right, and in this example 'on')
 - (When you edit a bit field in phpmyadmin, it also shows it like this.)
 - */
 - function dectobits($dec) {
 - $bin = (string)decbin($dec);
 - $length = 8; // 8-bit
 - return str_pad($bin, $length, "0", STR_PAD_LEFT);
 - }
 - //add your mdb2 connect stuff here...
 - $mdb2->setFetchMode(MDB2_FETCHMODE_OBJECT);
 - $sql = 'SELECT * FROM `debugbits` ORDER BY `id`';
 - $res = $mdb2->query($sql);
 - if(PEAR::IsError($res)) {
 - die($res->getMessage().' - '.$res->getUserinfo());
 - }
 - while($row = $res->fetchRow()) {
 - echo $row->id.' [ascii value: '.ord($row->mybit).' ] '.dectobits(ord($row->mybit)).' : '.$row->text.'<br/>';
 - }
 - echo '<hr><br/>';
 - //using php's mysql_query to show how it should work, same sql, same echo:
 - $sql = 'SELECT * FROM `debugbits` ORDER BY `id`';
 - $res = mysql_query($sql) or die(mysql_error());
 - while($row = mysql_fetch_object($res)) {
 - echo $row->id.' [ascii value: '.ord($row->mybit).' ] '.dectobits(ord($row->mybit)).' : '.$row->text.'<br/>';
 - }
 - /*
 - //MDB2 results with mysql driver, row 6 and 11 are wrong:
 - 1 [ascii value: 1 ] 00000001 : one
 - 2 [ascii value: 2 ] 00000010 : two
 - 3 [ascii value: 4 ] 00000100 : three
 - 4 [ascii value: 8 ] 00001000 : four
 - 5 [ascii value: 16 ] 00010000 : five
 - 6 [ascii value: 0 ] 00000000 : six <<<--- zero!
 - 7 [ascii value: 64 ] 01000000 : seven
 - 8 [ascii value: 128 ] 10000000 : eight
 - 9 [ascii value: 3 ] 00000011 : one and two
 - 10 [ascii value: 5 ] 00000101 : one and three
 - 11 [ascii value: 0 ] 00000000 : one and four <<<--- zero!
 - 12 [ascii value: 17 ] 00010001 : one and five
 - 13 [ascii value: 33 ] 00100001 : one and six
 - 14 [ascii value: 65 ] 01000001 : one and seven
 - 15 [ascii value: 129 ] 10000001 : one and eight
 - //MDB2 results with mysqli driver:
 - //edit: turns out the mysqli driver already returns bit fields as their ascii/decimal value and it's no longer required to use php's ord() function to do this. The mysqli driver' results are correct, and the same as the expected results below.
 - //expected results (like as shown by php's mysql_query())
 - 1 [ascii value: 1 ] 00000001 : one
 - 2 [ascii value: 2 ] 00000010 : two
 - 3 [ascii value: 4 ] 00000100 : three
 - 4 [ascii value: 8 ] 00001000 : four
 - 5 [ascii value: 16 ] 00010000 : five
 - 6 [ascii value: 32 ] 00100000 : six
 - 7 [ascii value: 64 ] 01000000 : seven
 - 8 [ascii value: 128 ] 10000000 : eight
 - 9 [ascii value: 3 ] 00000011 : one and two
 - 10 [ascii value: 5 ] 00000101 : one and three
 - 11 [ascii value: 9 ] 00001001 : one and four
 - 12 [ascii value: 17 ] 00010001 : one and five
 - 13 [ascii value: 33 ] 00100001 : one and six
 - 14 [ascii value: 65 ] 01000001 : one and seven
 - 15 [ascii value: 129 ] 10000001 : one and eight
 - */
 - ?>
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment