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