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