Skip to content

Commit 294e7c2

Browse files
committed
Annother attempt at fixing the mysqli_fetch_field tests
Instead of character set detection (which doesn't always work correctly) fetch the character set info using mysqli_get_charset(). To make sure that the returned info applies to all of client, connection and result explicitely set utf8 as charset using mysqli_set_charset() before. I'm not sure whether that last part is really necessary, but included it to be safe.
1 parent f2a8912 commit 294e7c2

File tree

6 files changed

+64
-135
lines changed

6 files changed

+64
-135
lines changed

ext/mysqli/tests/connect.inc

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -129,99 +129,6 @@
129129
}
130130
}
131131

132-
function my_get_charsets($link) {
133-
134-
/* Those tree are set by SET NAMES */
135-
$charsets = array(
136-
'client' => NULL,
137-
'results' => NULL,
138-
'connection' => NULL,
139-
);
140-
141-
if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE '%character%'"))) {
142-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
143-
return $charsets;
144-
}
145-
146-
$names = array();
147-
while ($row = mysqli_fetch_assoc($res)) {
148-
$names[$row['Variable_name']] = $row['Value'];
149-
}
150-
mysqli_free_result($res);
151-
152-
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_client']))) ||
153-
!($details = mysqli_fetch_assoc($res))) {
154-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
155-
return $charsets;
156-
}
157-
mysqli_free_result($res);
158-
159-
$charsets['client'] = array(
160-
'charset' => $details['Charset'],
161-
'desc' => $details['Description'],
162-
'collation' => $details['Default collation'],
163-
'maxlen' => $details['Maxlen'],
164-
'nr' => NULL,
165-
);
166-
167-
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
168-
!($collation = mysqli_fetch_assoc($res))) {
169-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
170-
return $charsets;
171-
}
172-
mysqli_free_result($res);
173-
$charsets['client']['nr'] = $collation['Id'];
174-
175-
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_results']))) ||
176-
!($details = mysqli_fetch_assoc($res))) {
177-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
178-
return $charsets;
179-
}
180-
mysqli_free_result($res);
181-
182-
$charsets['results'] = array(
183-
'charset' => $details['Charset'],
184-
'desc' => $details['Description'],
185-
'collation' => $details['Default collation'],
186-
'maxlen' => $details['Maxlen'],
187-
'nr' => NULL,
188-
);
189-
190-
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
191-
!($collation = mysqli_fetch_assoc($res))) {
192-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
193-
return $charsets;
194-
}
195-
mysqli_free_result($res);
196-
$charsets['results']['nr'] = $collation['Id'];
197-
198-
199-
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_connection']))) ||
200-
!($details = mysqli_fetch_assoc($res))) {
201-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
202-
return $charsets;
203-
}
204-
mysqli_free_result($res);
205-
206-
$charsets['connection'] = array(
207-
'charset' => $details['Charset'],
208-
'desc' => $details['Description'],
209-
'collation' => $details['Default collation'],
210-
'maxlen' => $details['Maxlen'],
211-
'nr' => NULL,
212-
);
213-
214-
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
215-
!($collation = mysqli_fetch_assoc($res))) {
216-
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
217-
return $charsets;
218-
}
219-
mysqli_free_result($res);
220-
$charsets['connection']['nr'] = $collation['Id'];
221-
222-
return $charsets;
223-
}
224-
225132
function have_innodb($link) {
226133
if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
227134
($row = $res->fetch_row()) &&

ext/mysqli/tests/mysqli_fetch_field.phpt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ require_once('skipifconnectfailure.inc');
2222

2323
require('table.inc');
2424

25-
$charsets = my_get_charsets($link);
25+
// Make sure that client, connection and result charsets are all the
26+
// same. Not sure whether this is strictly necessary.
27+
if (!mysqli_set_charset($link, 'utf8'))
28+
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
29+
30+
$charsetInfo = mysqli_get_charset($link);
31+
2632
if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
2733
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
2834
}
@@ -34,19 +40,17 @@ require_once('skipifconnectfailure.inc');
3440
/* label column, result set charset */
3541
$tmp = mysqli_fetch_field($res);
3642
var_dump($tmp);
37-
if ($tmp->charsetnr != $charsets['results']['nr']) {
43+
if ($tmp->charsetnr != $charsetInfo->number) {
3844
printf("[004] Expecting charset %s/%d got %d\n",
39-
$charsets['results']['charset'],
40-
$charsets['results']['nr'], $tmp->charsetnr);
45+
$charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr);
4146
}
42-
if ($tmp->length != (1 * $charsets['results']['maxlen'])) {
47+
if ($tmp->length != $charsetInfo->max_length) {
4348
printf("[005] Expecting length %d got %d\n",
44-
$charsets['results']['maxlen'],
45-
$tmp->max_length);
49+
$charsetInfo->max_length, $tmp->max_length);
4650
}
4751
if ($tmp->db != $db) {
4852
printf("011] Expecting database '%s' got '%s'\n",
49-
$db, $tmp->db);
53+
$db, $tmp->db);
5054
}
5155

5256
var_dump(mysqli_fetch_field($res));
@@ -174,4 +178,4 @@ object(stdClass)#%d (13) {
174178
[%u|b%"decimals"]=>
175179
int(0)
176180
}
177-
done!
181+
done!

ext/mysqli/tests/mysqli_fetch_field_oo.phpt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ require_once('skipifconnectfailure.inc');
2727
if (!is_null($tmp = @$res->fetch_field($link)))
2828
printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
2929

30-
$charsets = my_get_charsets($link);
30+
// Make sure that client, connection and result charsets are all the
31+
// same. Not sure whether this is strictly necessary.
32+
if (!$mysqli->set_charset('utf8'))
33+
printf("[%d] %s\n", $mysqli->errno, $mysqli->errno);
34+
35+
$charsetInfo = $mysqli->get_charset();
3136

3237
if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
3338
printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error);
@@ -37,18 +42,16 @@ require_once('skipifconnectfailure.inc');
3742

3843
$tmp = $res->fetch_field();
3944
var_dump($tmp);
40-
if ($tmp->charsetnr != $charsets['results']['nr']) {
45+
if ($tmp->charsetnr != $charsetInfo->number) {
4146
printf("[005] Expecting charset %s/%d got %d\n",
42-
$charsets['results']['charset'],
43-
$charsets['results']['nr'], $tmp->charsetnr);
47+
$charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr);
4448
}
45-
if ($tmp->length != (1 * $charsets['results']['maxlen'])) {
49+
if ($tmp->length != $charsetInfo->max_length) {
4650
printf("[006] Expecting length %d got %d\n",
47-
$charsets['results']['maxlen'],
48-
$tmp->max_length);
51+
$charsetInfo->max_length, $tmp->max_length);
4952
}
5053
if ($tmp->db != $db) {
51-
printf("008] Expecting database '%s' got '%s'\n",
54+
printf("[007] Expecting database '%s' got '%s'\n",
5255
$db, $tmp->db);
5356
}
5457

@@ -126,4 +129,4 @@ object(stdClass)#%d (13) {
126129
bool(false)
127130

128131
Warning: mysqli_result::fetch_field(): Couldn't fetch mysqli_result in %s on line %d
129-
done!
132+
done!

ext/mysqli/tests/mysqli_fetch_fields.phpt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ require_once('skipifconnectfailure.inc');
2121
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
2222

2323
require('table.inc');
24-
$charsets = my_get_charsets($link);
24+
25+
// Make sure that client, connection and result charsets are all the
26+
// same. Not sure whether this is strictly necessary.
27+
if (!mysqli_set_charset($link, 'utf8'))
28+
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
29+
30+
$charsetInfo = mysqli_get_charset($link);
2531

2632
if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
2733
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
@@ -33,14 +39,14 @@ require_once('skipifconnectfailure.inc');
3339
switch ($k) {
3440
case 1:
3541
/* label column, result set charset */
36-
if ($field->charsetnr != $charsets['results']['nr']) {
42+
if ($field->charsetnr != $charsetInfo->number) {
3743
printf("[004] Expecting charset %s/%d got %d\n",
38-
$charsets['results']['charset'],
39-
$charsets['results']['nr'], $field->charsetnr);
44+
$charsetInfo->charset,
45+
$charsetInfo->number, $field->charsetnr);
4046
}
41-
if ($field->length != (1 * $charsets['results']['maxlen'])) {
47+
if ($field->length != $charsetInfo->max_length) {
4248
printf("[005] Expecting length %d got %d\n",
43-
$charsets['results']['maxlen'],
49+
$charsetInfo->max_length,
4450
$field->max_length);
4551
}
4652
break;
@@ -118,4 +124,4 @@ object(stdClass)#%d (13) {
118124
}
119125

120126
Warning: mysqli_fetch_fields(): Couldn't fetch mysqli_result in %s on line %d
121-
done!
127+
done!

ext/mysqli/tests/mysqli_field_seek.phpt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ require_once('skipifconnectfailure.inc');
6666
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
6767

6868
require('table.inc');
69-
$charsets = my_get_charsets($link);
69+
70+
// Make sure that client, connection and result charsets are all the
71+
// same. Not sure whether this is strictly necessary.
72+
if (!mysqli_set_charset($link, 'utf8'))
73+
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
74+
75+
$charsetInfo = mysqli_get_charset($link);
7076

7177
if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) {
7278
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
@@ -81,15 +87,13 @@ require_once('skipifconnectfailure.inc');
8187
$field = mysqli_fetch_field($res);
8288
var_dump($field);
8389
/* label column, result set charset */
84-
if ($field->charsetnr != $charsets['results']['nr']) {
90+
if ($field->charsetnr != $charsetInfo->number) {
8591
printf("[004] Expecting charset %s/%d got %d\n",
86-
$charsets['results']['charset'],
87-
$charsets['results']['nr'], $field->charsetnr);
92+
$charsetInfo->charset, $charsetInfo->number, $field->charsetnr);
8893
}
89-
if ($field->length != (1 * $charsets['results']['maxlen'])) {
94+
if ($field->length != $charsetInfo->max_length) {
9095
printf("[005] Expecting length %d got %d\n",
91-
$charsets['results']['maxlen'],
92-
$field->max_length);
96+
$charsetInfo->max_length, $field->max_length);
9397
}
9498

9599
var_dump(mysqli_field_tell($res));
@@ -217,7 +221,7 @@ bool(false)
217221
Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
218222
bool(false)
219223
bool(true)
220-
object(stdClass)#3 (13) {
224+
object(stdClass)#%d (13) {
221225
[%u|b%"name"]=>
222226
%unicode|string%(5) "_null"
223227
[%u|b%"orgname"]=>
@@ -248,4 +252,4 @@ object(stdClass)#3 (13) {
248252

249253
Warning: mysqli_field_seek(): Couldn't fetch mysqli_result in %s on line %d
250254
NULL
251-
done!
255+
done!

ext/mysqli/tests/mysqli_stmt_get_result_metadata_fetch_field.phpt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ if (!function_exists('mysqli_stmt_get_result'))
1212
--FILE--
1313
<?php
1414
require('table.inc');
15-
$charsets = my_get_charsets($link);
15+
16+
// Make sure that client, connection and result charsets are all the
17+
// same. Not sure whether this is strictly necessary.
18+
if (!mysqli_set_charset($link, 'utf8'))
19+
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
20+
21+
$charsetInfo = mysqli_get_charset($link);
1622

1723
if (!($stmt = mysqli_stmt_init($link)) ||
1824
!mysqli_stmt_prepare($stmt, "SELECT id, label, id + 1 as _id, concat(label, '_') ___label FROM test ORDER BY id ASC LIMIT 3") ||
@@ -39,15 +45,14 @@ if (!function_exists('mysqli_stmt_get_result'))
3945
Label column, result set charset.
4046
All of the following columns are "too hot" - too server dependent
4147
*/
42-
if ($field->charsetnr != $charsets['results']['nr']) {
48+
if ($field->charsetnr != $charsetInfo->number) {
4349
printf("[004] Expecting charset %s/%d got %d\n",
44-
$charsets['results']['charset'],
45-
$charsets['results']['nr'], $field->charsetnr);
50+
$charsetInfo->charset,
51+
$charsetInfo->number, $field->charsetnr);
4652
}
47-
if ($field->length != (1 * $charsets['results']['maxlen'])) {
53+
if ($field->length != $charsetInfo->max_length) {
4854
printf("[005] Expecting length %d got %d\n",
49-
$charsets['results']['maxlen'],
50-
$field->max_length);
55+
$charsetInfo->max_length, $field->max_length);
5156
}
5257
}
5358
}
@@ -173,4 +178,4 @@ object(stdClass)#%d (13) {
173178
[%u|b%"decimals"]=>
174179
int(31)
175180
}
176-
done!
181+
done!

0 commit comments

Comments
 (0)