Advertisement
Mav

MDB2 bugreport

Mav
Feb 23rd, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. <?php
  2. /*
  3. --
  4. -- Table structure for table `debug`
  5. --
  6.  
  7. CREATE TABLE IF NOT EXISTS `debugbits` (
  8.   `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  9.   `text` varchar(64) NOT NULL,
  10.   `mybit` bit(8) NOT NULL,
  11.   PRIMARY KEY (`id`)
  12. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
  13.  
  14. --
  15. -- Dumping data for table `debug`
  16. --
  17.  
  18. INSERT INTO `debugbits` (`id`, `text`, `mybit`) VALUES
  19. (1, 'one', b '00000001'),
  20. (2, 'two', b '00000010'),
  21. (3, 'three', b '00000100'),
  22. (4, 'four', b '00001000'),
  23. (5, 'five', b '00010000'),
  24. (6, 'six', b '00100000'),
  25. (7, 'seven',  b '01000000'),
  26. (8, 'eight',  b '10000000'),
  27. (9, 'one and two', b '00000011'),
  28. (10, 'one and three', b '00000101'),
  29. (11, 'one and four', b '00001001'),
  30. (12, 'one and five', b '00010001'),
  31. (13, 'one and six', b '00100001'),
  32. (14, 'one and seven', b '01000001'),
  33. (15, 'one and eight', b '10000001');
  34. */
  35.  
  36.  
  37.  
  38. /*
  39.  This function converts the bits to a more readable string of ones and zeros,
  40.  like 00000001 (note that the first bit is on the right, and in this example 'on')
  41.  (When you edit a bit field in phpmyadmin, it also shows it like this.)
  42. */
  43. function dectobits($dec) {
  44.     $bin = (string)decbin($dec);
  45.     $length = 8; // 8-bit
  46.     return str_pad($bin, $length, "0", STR_PAD_LEFT);
  47. }
  48.  
  49.  
  50. //add your mdb2 connect stuff here...
  51. $mdb2->setFetchMode(MDB2_FETCHMODE_OBJECT);
  52.  
  53. $sql = 'SELECT * FROM `debugbits` ORDER BY `id`';
  54. $res = $mdb2->query($sql);
  55. if(PEAR::IsError($res)) {
  56.     die($res->getMessage().' - '.$res->getUserinfo());
  57. }
  58. while($row = $res->fetchRow()) {
  59.     echo $row->id.' [ascii value: '.ord($row->mybit).' ] '.dectobits(ord($row->mybit)).' : '.$row->text.'<br/>';
  60. }
  61.  
  62. echo '<hr><br/>';
  63.  
  64. //using php's mysql_query to show how it should work, same sql, same echo:
  65. $sql = 'SELECT * FROM `debugbits` ORDER BY `id`';
  66. $res = mysql_query($sql) or die(mysql_error());
  67. while($row = mysql_fetch_object($res)) {
  68.     echo $row->id.' [ascii value: '.ord($row->mybit).' ] '.dectobits(ord($row->mybit)).' : '.$row->text.'<br/>';
  69. }
  70.  
  71.  
  72. /*
  73. //MDB2 results with mysql driver, row 6 and 11 are wrong:
  74. 1 [ascii value: 1 ] 00000001 : one
  75. 2 [ascii value: 2 ] 00000010 : two
  76. 3 [ascii value: 4 ] 00000100 : three
  77. 4 [ascii value: 8 ] 00001000 : four
  78. 5 [ascii value: 16 ] 00010000 : five
  79. 6 [ascii value: 0 ] 00000000 : six <<<--- zero!
  80. 7 [ascii value: 64 ] 01000000 : seven
  81. 8 [ascii value: 128 ] 10000000 : eight
  82. 9 [ascii value: 3 ] 00000011 : one and two
  83. 10 [ascii value: 5 ] 00000101 : one and three
  84. 11 [ascii value: 0 ] 00000000 : one and four  <<<--- zero!
  85. 12 [ascii value: 17 ] 00010001 : one and five
  86. 13 [ascii value: 33 ] 00100001 : one and six
  87. 14 [ascii value: 65 ] 01000001 : one and seven
  88. 15 [ascii value: 129 ] 10000001 : one and eight
  89.  
  90. //MDB2 results with mysqli driver:
  91. //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.
  92.  
  93. //expected results (like as shown by php's mysql_query())
  94. 1 [ascii value: 1 ] 00000001 : one
  95. 2 [ascii value: 2 ] 00000010 : two
  96. 3 [ascii value: 4 ] 00000100 : three
  97. 4 [ascii value: 8 ] 00001000 : four
  98. 5 [ascii value: 16 ] 00010000 : five
  99. 6 [ascii value: 32 ] 00100000 : six
  100. 7 [ascii value: 64 ] 01000000 : seven
  101. 8 [ascii value: 128 ] 10000000 : eight
  102. 9 [ascii value: 3 ] 00000011 : one and two
  103. 10 [ascii value: 5 ] 00000101 : one and three
  104. 11 [ascii value: 9 ] 00001001 : one and four
  105. 12 [ascii value: 17 ] 00010001 : one and five
  106. 13 [ascii value: 33 ] 00100001 : one and six
  107. 14 [ascii value: 65 ] 01000001 : one and seven
  108. 15 [ascii value: 129 ] 10000001 : one and eight
  109. */
  110. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement