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