1--TEST-- 2mysqli_fetch_assoc() - BIT 3--EXTENSIONS-- 4mysqli 5--SKIPIF-- 6<?php 7require_once __DIR__ . '/test_setup/test_helpers.inc';; 8$link = mysqli_connect_or_skip(); 9if (mysqli_get_server_version($link) < 50003) 10 // b'001' syntax not supported before 5.0.3 11 die("skip Syntax used for test not supported with MySQL Server before 5.0.3"); 12?> 13--FILE-- 14<?php 15 require 'connect.inc'; 16 17 function dec32bin($dec, $bits) { 18 19 $maxval = pow(2, $bits); 20 $bin = ''; 21 for ($bitval = $maxval; $bitval >= 1; $bitval = $bitval / 2) { 22 if (($dec / $bitval) >= 1) { 23 $bin .= '1'; 24 $dec -= $bitval; 25 } else { 26 $bin .= '0'; 27 } 28 } 29 return $bin; 30 } 31 32 if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) 33 printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", 34 $host, $user, $db, $port, $socket); 35 36 for ($bits = 1; $bits <= 46; $bits++) { 37 if (1 == $bits) 38 $max_value = 1; 39 else 40 $max_value = pow(2, $bits) - 1; 41 if (!mysqli_query($link, "DROP TABLE IF EXISTS test") || 42 !mysqli_query($link, $sql = sprintf('CREATE TABLE test(id BIGINT, bit_value BIT(%d) NOT NULL, bit_null BIT(%d) DEFAULT NULL) ENGINE="%s"', $bits, $bits, $engine))) 43 printf("[002 - %d] [%d] %s\n",$bits, mysqli_errno($link), mysqli_error($link)); 44 45 $tests = 0; 46 $rand_max = mt_getrandmax(); 47 while ($tests < 10) { 48 49 $tests++; 50 if (1 == $tests) 51 $value = 0; 52 else if (2 == $tests) 53 $value = $max_value; 54 else { 55 if ($max_value > $rand_max) { 56 $max_loops = floor($max_value/$rand_max); 57 $num_loops = mt_rand(1, $max_loops); 58 $value = 0; 59 for ($i = 0; $i < $num_loops; $i++) 60 $value += mt_rand(0, $rand_max); 61 } else { 62 $value = mt_rand(0, $max_value); 63 } 64 } 65 66 $bin = ($bits < 32) ? decbin($value) : dec32bin($value, $bits); 67 $sql = sprintf("INSERT INTO test(id, bit_value) VALUES (%s, b'%s')", $value, $bin); 68 for ($i = 0; ($i < strlen($bin)) && ($bin[$i] == '0'); $i++) 69 ; 70 $bin2 = substr($bin, $i, strlen($bin)); 71 72 if (!mysqli_query($link, $sql)) 73 printf("[003 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link)); 74 75 $sql = sprintf("SELECT id, BIN(bit_value) AS _bin, bit_value + 0 AS _bit_value0, bit_value, bit_null FROM test WHERE id = %s", $value); 76 if (!$res = mysqli_query($link, $sql)) 77 printf("[004 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link)); 78 79 if (!$row = mysqli_fetch_assoc($res)) 80 printf("[005 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link)); 81 82 if (($value != $row['id']) || (($bin != $row['_bin']) && ($bin2 != $row['_bin']))) { 83 debug_zval_dump($row); 84 printf("[006 - %d] Insert of %s in BIT(%d) column might have failed. id = %s, bin = %s (%s/%s)\n", 85 $bits, $value, $bits, $row['id'], $row['_bin'], $bin, $bin2); 86 break; 87 } 88 if ($value != $row['bit_value']) { 89 debug_zval_dump($row); 90 printf("%10s %64s\n%10s %64s\n", '_bin', $row['_bin'], 'insert', $bin); 91 printf("[007 - %d] Expecting %s got %s\n", $bits, $value, $row['bit_value']); 92 break; 93 } 94 95 if (null !== $row['bit_null']) { 96 debug_zval_dump($row); 97 printf("[008 - %d] Expecting null got %s/%s\n", $bits, gettype($row['bit_value']), $row['bit_value']); 98 break; 99 } 100 } 101 } 102 103 mysqli_close($link); 104 print "done!"; 105?> 106--CLEAN-- 107<?php 108require_once 'clean_table.inc'; 109?> 110--EXPECT-- 111done! 112