@@ -43,57 +43,66 @@ if (mysqli_get_server_version($link) <= 40100) {
43
43
}
44
44
$ stmt = null ;
45
45
46
- // 3. param number has to match - missing all parameters
46
+ // 3. Too many parameters
47
+ $ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
48
+ try {
49
+ $ stmt ->execute ([&$ abc , null , $ id , 24 ]);
50
+ } catch (ArgumentCountError $ e ) {
51
+ echo '[002] ' .$ e ->getMessage ()."\n" ;
52
+ }
53
+ $ stmt = null ;
54
+
55
+ // 4. param number has to match - missing all parameters
47
56
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
48
57
try {
49
58
$ stmt ->execute ([]);
50
59
} catch (mysqli_sql_exception $ e ) {
51
- echo '[002 ] ' .$ e ->getMessage ()."\n" ;
60
+ echo '[003 ] ' .$ e ->getMessage ()."\n" ;
52
61
}
53
62
$ stmt = null ;
54
63
55
- // 4 . param number has to match - missing argument to execute()
64
+ // 5 . param number has to match - missing argument to execute()
56
65
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
57
66
try {
58
67
$ stmt ->execute ();
59
68
} catch (mysqli_sql_exception $ e ) {
60
- echo '[003 ] ' .$ e ->getMessage ()."\n" ;
69
+ echo '[004 ] ' .$ e ->getMessage ()."\n" ;
61
70
}
62
71
$ stmt = null ;
63
72
64
- // 5 . wrong argument to execute()
73
+ // 6 . wrong argument to execute()
65
74
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
66
75
try {
67
76
$ stmt ->execute (42 );
68
77
} catch (TypeError $ e ) {
69
- echo '[004 ] ' .$ e ->getMessage ()."\n" ;
78
+ echo '[005 ] ' .$ e ->getMessage ()."\n" ;
70
79
}
71
80
$ stmt = null ;
72
81
73
- // 6 . objects are not arrays and are not accepted
82
+ // 7 . objects are not arrays and are not accepted
74
83
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
75
84
try {
76
85
$ stmt ->execute ((object )[&$ abc , 42 , $ id ]);
77
86
} catch (TypeError $ e ) {
78
- echo '[005 ] ' .$ e ->getMessage ()."\n" ;
87
+ echo '[006 ] ' .$ e ->getMessage ()."\n" ;
79
88
}
80
89
$ stmt = null ;
81
90
82
- // 7 . arrays by reference work too
91
+ // 8 . arrays by reference work too
83
92
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
84
93
$ arr = [&$ abc , 42 , $ id ];
85
94
$ arr2 = &$ arr ;
86
95
$ stmt ->execute ($ arr2 );
87
96
assert ($ stmt ->get_result ()->fetch_assoc () === ['label ' =>'a ' , 'anon ' =>'abc ' , 'num ' => '42 ' ]);
88
97
$ stmt = null ;
89
98
90
- // 8 . no placeholders in statement. params are ignored
99
+ // 9 . no placeholders in statement. nothing to bind in an empty array
91
100
$ stmt = $ link ->prepare ('SELECT label FROM test WHERE id=1 ' );
92
- $ stmt ->execute ([' I am ignored ' ]);
101
+ $ stmt ->execute ([]);
93
102
assert ($ stmt ->get_result ()->fetch_assoc () === ['label ' =>'a ' ]);
94
103
$ stmt = null ;
95
104
96
- // 9 . once bound the values are persisted. Just like in PDO
105
+ // 10 . once bound the values are persisted. Just like in PDO
97
106
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
98
107
$ stmt ->execute (['abc ' , 42 , $ id ]);
99
108
assert ($ stmt ->get_result ()->fetch_assoc () === ['label ' =>'a ' , 'anon ' =>'abc ' , 'num ' => '42 ' ]);
@@ -102,29 +111,23 @@ if (mysqli_get_server_version($link) <= 40100) {
102
111
try {
103
112
$ stmt ->execute ([]); // no params here. PDO doesn't throw an error, but mysqli does
104
113
} catch (mysqli_sql_exception $ e ) {
105
- echo '[006 ] ' .$ e ->getMessage ()."\n" ;
114
+ echo '[007 ] ' .$ e ->getMessage ()."\n" ;
106
115
}
107
116
$ stmt = null ;
108
117
109
- // 10 . mixing binding styles not possible. Also, NULL should stay NULL when bound as string
118
+ // 11 . mixing binding styles not possible. Also, NULL should stay NULL when bound as string
110
119
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
111
120
$ stmt ->bind_param ('sss ' , ...['abc ' , 42 , null ]);
112
121
$ stmt ->execute ([null , null , $ id ]);
113
122
assert ($ stmt ->get_result ()->fetch_assoc () === ['label ' =>'a ' , 'anon ' =>null , 'num ' => null ]);
114
123
$ stmt = null ;
115
124
116
- // 11 . array keys are ignored. Even numerical indices are not considered (PDO does a weird thing with the numerical indices)
125
+ // 12 . array keys are ignored. Even numerical indices are not considered (PDO does a weird thing with the numerical indices)
117
126
$ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
118
127
$ stmt ->execute (['A ' =>'abc ' , 2 =>42 , null =>$ id ]);
119
128
assert ($ stmt ->get_result ()->fetch_assoc () === ['label ' =>'a ' , 'anon ' =>'abc ' , 'num ' => '42 ' ]);
120
129
$ stmt = null ;
121
130
122
- // 12. Too many parameters is not a problem. The redundant ones just get ignored
123
- $ stmt = $ link ->prepare ('SELECT label, ? AS anon, ? AS num FROM test WHERE id=? ' );
124
- $ stmt ->execute (['abc ' , null , $ id , 42 ]);
125
- assert ($ stmt ->get_result ()->fetch_assoc () === ['label ' =>'a ' , 'anon ' =>'abc ' , 'num ' => null ]);
126
- $ stmt = null ;
127
-
128
131
129
132
mysqli_close ($ link );
130
133
print "done! " ;
@@ -135,9 +138,10 @@ if (mysqli_get_server_version($link) <= 40100) {
135
138
?>
136
139
--EXPECT--
137
140
[001] No data supplied for 1 parameter in prepared statement
138
- [002] No data supplied for 3 parameters in prepared statement
139
- [003] No data supplied for parameters in prepared statement
140
- [004] mysqli_stmt::execute(): Argument #1 ($params) must be of type ?array, int given
141
- [005] mysqli_stmt::execute(): Argument #1 ($params) must be of type ?array, stdClass given
142
- [006] No data supplied for 3 parameters in prepared statement
141
+ [002] The number of values must match the number of parameters in the prepared statement
142
+ [003] No data supplied for 3 parameters in prepared statement
143
+ [004] No data supplied for parameters in prepared statement
144
+ [005] mysqli_stmt::execute(): Argument #1 ($params) must be of type ?array, int given
145
+ [006] mysqli_stmt::execute(): Argument #1 ($params) must be of type ?array, stdClass given
146
+ [007] No data supplied for 3 parameters in prepared statement
143
147
done!
0 commit comments