1
+ --TEST--
2
+ GitHub Issue #35 binary encoding error when binding by name
3
+ --DESCRIPTION--
4
+ Based on pdo_035_binary_encoding_error_bound_by_name.phpt but this includes error checking for various encoding errors
5
+ --SKIPIF--
6
+ <?php require ('skipif_mid-refactor.inc ' ); ?>
7
+ --FILE--
8
+ <?php
9
+
10
+ function bindTypeNoEncoding ($ conn , $ sql , $ input )
11
+ {
12
+ try {
13
+ $ value = 1 ;
14
+
15
+ $ stmt = $ conn ->prepare ($ sql );
16
+ $ stmt ->bindParam (1 , $ value , PDO ::PARAM_INT , 0 , PDO ::SQLSRV_ENCODING_DEFAULT );
17
+ $ stmt ->setAttribute (constant ('PDO::SQLSRV_ATTR_ENCODING ' ), PDO ::SQLSRV_ENCODING_BINARY );
18
+ $ stmt ->bindParam (2 , $ input , PDO ::PARAM_LOB );
19
+ $ stmt ->execute ();
20
+ } catch (PDOException $ e ) {
21
+ $ error = '*An encoding was specified for parameter 1. Only PDO::PARAM_LOB and PDO::PARAM_STR can take an encoding option. ' ;
22
+ if (!fnmatch ($ error , $ e ->getMessage ())) {
23
+ echo "Error message unexpected in bindTypeNoEncoding \n" ;
24
+ var_dump ($ e ->getMessage ());
25
+ }
26
+ }
27
+ }
28
+
29
+ function bindDefaultEncoding ($ conn , $ sql , $ input )
30
+ {
31
+ try {
32
+ $ value = 1 ;
33
+
34
+ $ stmt = $ conn ->prepare ($ sql );
35
+ $ stmt ->bindParam (1 , $ value , PDO ::PARAM_STR , 0 , PDO ::SQLSRV_ENCODING_DEFAULT );
36
+ $ stmt ->setAttribute (constant ('PDO::SQLSRV_ATTR_ENCODING ' ), PDO ::SQLSRV_ENCODING_BINARY );
37
+ $ stmt ->bindParam (2 , $ input , PDO ::PARAM_LOB );
38
+ $ stmt ->execute ();
39
+ } catch (PDOException $ e ) {
40
+ $ error = '*Invalid encoding specified for parameter 1. ' ;
41
+ if (!fnmatch ($ error , $ e ->getMessage ())) {
42
+ echo "Error message unexpected in bindDefaultEncoding \n" ;
43
+ var_dump ($ e ->getMessage ());
44
+ }
45
+ }
46
+ }
47
+
48
+ function insertData ($ conn , $ sql , $ input )
49
+ {
50
+ try {
51
+ $ value = 1 ;
52
+
53
+ $ stmt = $ conn ->prepare ($ sql );
54
+ $ stmt ->bindParam (1 , $ value );
55
+ $ stmt ->setAttribute (constant ('PDO::SQLSRV_ATTR_ENCODING ' ), PDO ::SQLSRV_ENCODING_BINARY );
56
+ $ stmt ->bindParam (2 , $ input , PDO ::PARAM_LOB );
57
+ $ stmt ->execute ();
58
+ } catch (PDOException $ e ) {
59
+ echo "Error unexpected in insertData \n" ;
60
+ var_dump ($ e ->getMessage ());
61
+ }
62
+ }
63
+
64
+ function invalidEncoding1 ($ conn , $ sql )
65
+ {
66
+ try {
67
+ $ stmt = $ conn ->prepare ($ sql );
68
+ $ stmt ->bindColumn (1 , $ id , PDO ::PARAM_INT , 0 , PDO ::SQLSRV_ENCODING_UTF8 );
69
+ $ stmt ->execute ();
70
+ $ stmt ->fetch (PDO ::FETCH_BOUND );
71
+ } catch (PDOException $ e ) {
72
+ $ error = '*An encoding was specified for column 1. Only PDO::PARAM_LOB and PDO::PARAM_STR column types can take an encoding option. ' ;
73
+ if (!fnmatch ($ error , $ e ->getMessage ())) {
74
+ echo "Error message unexpected in invalidEncoding1 \n" ;
75
+ var_dump ($ e ->getMessage ());
76
+ }
77
+ }
78
+ }
79
+
80
+ function invalidEncoding2 ($ conn , $ sql )
81
+ {
82
+ try {
83
+ $ stmt = $ conn ->prepare ($ sql );
84
+ $ stmt ->bindColumn ('Value ' , $ val1 , PDO ::PARAM_LOB , 0 , PDO ::SQLSRV_ENCODING_DEFAULT );
85
+ $ stmt ->execute ();
86
+ $ stmt ->fetch (PDO ::FETCH_BOUND );
87
+ } catch (PDOException $ e ) {
88
+ $ error = '*Invalid encoding specified for column 1. ' ;
89
+ if (!fnmatch ($ error , $ e ->getMessage ())) {
90
+ echo "Error message unexpected in invalidEncoding2 \n" ;
91
+ var_dump ($ e ->getMessage ());
92
+ }
93
+ }
94
+ }
95
+
96
+ function invalidEncoding3 ($ conn , $ sql )
97
+ {
98
+ try {
99
+ $ stmt = $ conn ->prepare ($ sql );
100
+ $ stmt ->bindColumn (1 , $ id , PDO ::PARAM_STR , 0 , "dummy " );
101
+ $ stmt ->execute ();
102
+ $ stmt ->fetch (PDO ::FETCH_BOUND );
103
+ } catch (PDOException $ e ) {
104
+ $ error = '*An invalid type or value was given as bound column driver data for column 1. Only encoding constants such as PDO::SQLSRV_ENCODING_UTF8 may be used as bound column driver data. ' ;
105
+ if (!fnmatch ($ error , $ e ->getMessage ())) {
106
+ echo "Error message unexpected in invalidEncoding3 \n" ;
107
+ var_dump ($ e ->getMessage ());
108
+ }
109
+ }
110
+ }
111
+
112
+ try {
113
+ require_once ( "MsCommon_mid-refactor.inc " );
114
+
115
+ // Connect
116
+ $ conn = connect ();
117
+
118
+ // Create a table
119
+ $ tableName = "testTableIssue35 " ;
120
+ createTable ($ conn , $ tableName , array ("ID " => "int " , "Value " => "varbinary(max) " ));
121
+
122
+ // Insert data using bind parameters
123
+ $ sql = "INSERT INTO $ tableName VALUES (?, ?) " ;
124
+ $ message = "This is to test github issue 35. " ;
125
+ $ value = base64_encode ($ message );
126
+
127
+ // Errors expected
128
+ bindTypeNoEncoding ($ conn , $ sql , $ value );
129
+ bindDefaultEncoding ($ conn , $ sql , $ value );
130
+
131
+ // No error expected
132
+ insertData ($ conn , $ sql , $ value );
133
+
134
+ // Fetch data, but test several invalid encoding issues (errors expected)
135
+ $ sql = "SELECT * FROM $ tableName " ;
136
+ invalidEncoding1 ($ conn , $ sql );
137
+ invalidEncoding2 ($ conn , $ sql );
138
+ invalidEncoding3 ($ conn , $ sql );
139
+
140
+ // Now fetch it back
141
+ $ stmt = $ conn ->prepare ("SELECT Value FROM $ tableName " );
142
+ $ stmt ->bindColumn ('Value ' , $ val1 , PDO ::PARAM_LOB , 0 , PDO ::SQLSRV_ENCODING_BINARY );
143
+ $ stmt ->execute ();
144
+ $ stmt ->fetch (PDO ::FETCH_BOUND );
145
+ var_dump ($ val1 === $ value );
146
+
147
+ // Close connection
148
+ dropTable ($ conn , $ tableName );
149
+ unset($ stmt );
150
+ unset($ conn );
151
+ print "Done \n" ;
152
+ } catch (PDOException $ e ) {
153
+ var_dump ($ e ->errorInfo );
154
+ }
155
+ ?>
156
+ --EXPECT--
157
+ bool(true)
158
+ Done
0 commit comments