1--TEST--
2Fetching BIT column values using the PS API
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'connect.inc';
8if (!$link = @my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
9    die(sprintf("skip Can't connect to MySQL Server - [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
10}
11if (mysqli_get_server_version($link) < 50003) {
12    // b'001' syntax not supported before 5.0.3
13    die("skip Syntax used for test not supported with MySQL Server before 5.0.3");
14}
15?>
16--FILE--
17<?php
18    require 'connect.inc';
19
20    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
21        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
22            $host, $user, $db, $port, $socket);
23
24    /* NOTE: works only for up to 31 bits! This limitation should be documented. */
25    for ($bits = 1; $bits < 32; $bits++) {
26        $max_value = pow(2, $bits) - 1;
27        $tests = 0;
28        if (!mysqli_query($link, "DROP TABLE IF EXISTS test") ||
29            !mysqli_query($link, $sql = sprintf('CREATE TABLE test(id INT, label BIT(%d)) ENGINE="%s"', $bits, $engine)))
30            printf("[002 - %d] [%d] %s\n",$bits, mysqli_errno($link), mysqli_error($link));
31
32        if (!$stmt = mysqli_stmt_init($link))
33            printf("[003 - %d] [%d] %s\n", $bits, mysqli_errno($link), mysqli_error($link));
34
35        while ($tests < min($max_value, 20)) {
36            $tests++;
37            $value = mt_rand(0, $max_value);
38            $sql = sprintf("INSERT INTO test(id, label) VALUES (%d, b'%s')", $value, decbin($value));
39
40            if (!mysqli_stmt_prepare($stmt, $sql) ||
41                    !mysqli_stmt_execute($stmt))
42                printf("[004 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
43
44            $id = $_label0 = $label = null;
45            $sql = sprintf("SELECT id, label + 0 AS _label0, label FROM test WHERE id = %d", $value);
46            if (!mysqli_stmt_prepare($stmt, $sql) ||
47                    !mysqli_stmt_execute($stmt) ||
48                    !mysqli_stmt_bind_result($stmt, $id, $_label0, $label))
49                printf("[005 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
50
51            if (!mysqli_stmt_fetch($stmt))
52                printf("[006 - %d] [%d] %s\n", $bits, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
53
54            if (($id !== $_label0) || ($value !== $_label0)) {
55                printf("[007 - %d] Insert of %d in BIT(%d) column might have failed. MySQL reports odd values, id = %s, _label0 = %s, label = %s.\n", $bits, $value, $bits, $id, $_label0, $label);
56            }
57            if ($value != $label) {
58                printf("[008 - %d] Wrong values, (original) value = %s, id = %s,  label + 0 AS label0 = %s, label = %s\n",
59                    $bits, $value, $id, $_label0, $label);
60            }
61        }
62
63        mysqli_stmt_close($stmt);
64
65    }
66
67    mysqli_close($link);
68    print "done!";
69?>
70--CLEAN--
71<?php
72require_once 'clean_table.inc';
73?>
74--EXPECT--
75done!
76