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