summaryrefslogtreecommitdiff
path: root/ext/mysqli/tests/mysqli_stmt_bind_param_type_juggling.phpt
blob: b69dfe686e73d114e452ec845854921add6d7678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
--TEST--
mysqli_stmt_bind_param() - binding variable twice
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnectfailure.inc');
?>
--FILE--
<?php
    require('table.inc');

    function bind_twice($link, $engine, $sql_type1, $sql_type2, $bind_type1, $bind_type2, $bind_value1, $bind_value2, $offset) {

        if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
            printf("[%03d + 1] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
            return false;
        }
        mysqli_autocommit($link, true);

        $sql = sprintf("CREATE TABLE test(col1 %s, col2 %s) ENGINE=%s", $sql_type1, $sql_type2, $engine);
        if (!mysqli_query($link, $sql)) {
            printf("[%03d + 2] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
            return false;
        }

        if (!$stmt = mysqli_stmt_init($link)) {
            printf("[%03d + 3] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
            return false;
        }

        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(col1, col2) VALUES (?, ?)")) {
            printf("[%03d + 4] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }

        if (!mysqli_stmt_bind_param($stmt, $bind_type1 . $bind_type2, $bind_value1, $bind_value1)) {
            printf("[%03d + 5] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }

        if (!mysqli_stmt_execute($stmt)) {
            printf("[%03d + 6] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }

        if (!mysqli_stmt_bind_param($stmt, $bind_type1 . $bind_type2, $bind_value1, $bind_value2)) {
            printf("[%03d + 7] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }
        if (!mysqli_stmt_execute($stmt)) {
            printf("[%03d + 8] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
            return false;
        }

        mysqli_stmt_close($stmt);
        if (!$res = mysqli_query($link, "SELECT col1, col2 FROM test")) {
            printf("[%03d + 9] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
            return false;
        }

        if (2 !== ($tmp = mysqli_num_rows($res))) {
            printf("[%03d + 10] Expecting 2 rows, got %d rows [%d] %s\n", $offset, $tmp, mysqli_errno($link), mysqli_error($link));
        }

        $row = mysqli_fetch_assoc($res);
        if (($row['col1'] != $bind_value1) || ($row['col2'] != $bind_value1)) {
            printf("[%03d + 11] Expecting col1 = %s, col2 = %s got col1 = %s, col2 = %s - [%d] %s\n",
                $offset, $bind_value1, $bind_value1,
                $row['col1'], $row['col2'],
                mysqli_errno($link), mysqli_error($link));
            return false;
        }

        $row = mysqli_fetch_assoc($res);
        if (($row['col1'] != $bind_value1) || ($row['col2'] != $bind_value2)) {
            printf("[%03d + 12] Expecting col1 = %s, col2 = %s got col1 = %s, col2 = %s - [%d] %s\n",
                $offset, $bind_value1, $bind_value2,
                $row['col1'], $row['col2'],
                mysqli_errno($link), mysqli_error($link));
            return false;
        }
        mysqli_free_result($res);
        return true;
    }

    bind_twice($link, $engine, 'CHAR(1)', 'CHAR(1)', 's', 's', 'a', 'b', 10);
    bind_twice($link, $engine, 'INT', 'INT', 'i', 'i', 1, 2, 20);
    bind_twice($link, $engine, 'FLOAT', 'FLOAT', 'd', 'd', 1.01, 1.02, 30);

    /* type juggling - note that int->char works */
    bind_twice($link, $engine, 'CHAR(1)', 'CHAR(1)', 's', 's', 1, 2, 40);
    /* type juggling - note that string->integer works */
    bind_twice($link, $engine, 'INT', 'INT', 'i', 'i', '1', '2', 50);
    /* type juggling - note that string->float works*/
    bind_twice($link, $engine, 'FLOAT', 'FLOAT', 'd', 'd', '1.01', '1.02', 60);

    /* now, let's have two columns of different type and do type juggling */
    /*
    what the test will do is:
        1) col1 INT, col2 CHAR(1)
    2) bind_param('is', 1, 1)
    3) execute()
        4) bind_param('is', 1, 'a')
        5) execute()

        col1 INT, col2 INT
    bind_param('ii', '1', '2')	--> OK  (int column, string value)
        bind_param('ii', 1, 2) 			--> OK  (int column, int value)
    col1 CHAR(1), col2 CHAR(2)
    bind_param('ss', 1, 2)  		--> OK (string column, int value)

        So, what about:
        col1 INT, COL2 CHAR(1)
        bind_param('is', 1, 1)     ---> ??
    */
    bind_twice($link, $engine, 'INT', 'CHAR(1)', 'i', 's', 1, 'a', 70);

    mysqli_close($link);
    print "done!";
?>
--CLEAN--
<?php
	require_once("clean_table.inc");
?>
--EXPECT--
done!