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.
This commit is contained in:
Nikita Popov 2012-08-17 15:17:29 +02:00
parent f2a8912e61
commit 294e7c295f
6 changed files with 64 additions and 135 deletions

View File

@ -129,99 +129,6 @@
}
}
function my_get_charsets($link) {
/* Those tree are set by SET NAMES */
$charsets = array(
'client' => NULL,
'results' => NULL,
'connection' => NULL,
);
if (!($res = mysqli_query($link, "SHOW VARIABLES LIKE '%character%'"))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
$names = array();
while ($row = mysqli_fetch_assoc($res)) {
$names[$row['Variable_name']] = $row['Value'];
}
mysqli_free_result($res);
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_client']))) ||
!($details = mysqli_fetch_assoc($res))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
mysqli_free_result($res);
$charsets['client'] = array(
'charset' => $details['Charset'],
'desc' => $details['Description'],
'collation' => $details['Default collation'],
'maxlen' => $details['Maxlen'],
'nr' => NULL,
);
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
!($collation = mysqli_fetch_assoc($res))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
mysqli_free_result($res);
$charsets['client']['nr'] = $collation['Id'];
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_results']))) ||
!($details = mysqli_fetch_assoc($res))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
mysqli_free_result($res);
$charsets['results'] = array(
'charset' => $details['Charset'],
'desc' => $details['Description'],
'collation' => $details['Default collation'],
'maxlen' => $details['Maxlen'],
'nr' => NULL,
);
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
!($collation = mysqli_fetch_assoc($res))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
mysqli_free_result($res);
$charsets['results']['nr'] = $collation['Id'];
if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $names['character_set_connection']))) ||
!($details = mysqli_fetch_assoc($res))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
mysqli_free_result($res);
$charsets['connection'] = array(
'charset' => $details['Charset'],
'desc' => $details['Description'],
'collation' => $details['Default collation'],
'maxlen' => $details['Maxlen'],
'nr' => NULL,
);
if (!($res = mysqli_query($link, sprintf("SHOW COLLATION LIKE '%s'", $details['Default collation']))) ||
!($collation = mysqli_fetch_assoc($res))) {
printf("[%d] %s\n", mysqli_errno($link), mysqli_error($link));
return $charsets;
}
mysqli_free_result($res);
$charsets['connection']['nr'] = $collation['Id'];
return $charsets;
}
function have_innodb($link) {
if (($res = $link->query("SHOW VARIABLES LIKE 'have_innodb'")) &&
($row = $res->fetch_row()) &&

View File

@ -22,7 +22,13 @@ require_once('skipifconnectfailure.inc');
require('table.inc');
$charsets = my_get_charsets($link);
// Make sure that client, connection and result charsets are all the
// same. Not sure whether this is strictly necessary.
if (!mysqli_set_charset($link, 'utf8'))
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
$charsetInfo = mysqli_get_charset($link);
if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
}
@ -34,15 +40,13 @@ require_once('skipifconnectfailure.inc');
/* label column, result set charset */
$tmp = mysqli_fetch_field($res);
var_dump($tmp);
if ($tmp->charsetnr != $charsets['results']['nr']) {
if ($tmp->charsetnr != $charsetInfo->number) {
printf("[004] Expecting charset %s/%d got %d\n",
$charsets['results']['charset'],
$charsets['results']['nr'], $tmp->charsetnr);
$charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr);
}
if ($tmp->length != (1 * $charsets['results']['maxlen'])) {
if ($tmp->length != $charsetInfo->max_length) {
printf("[005] Expecting length %d got %d\n",
$charsets['results']['maxlen'],
$tmp->max_length);
$charsetInfo->max_length, $tmp->max_length);
}
if ($tmp->db != $db) {
printf("011] Expecting database '%s' got '%s'\n",

View File

@ -27,7 +27,12 @@ require_once('skipifconnectfailure.inc');
if (!is_null($tmp = @$res->fetch_field($link)))
printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
$charsets = my_get_charsets($link);
// Make sure that client, connection and result charsets are all the
// same. Not sure whether this is strictly necessary.
if (!$mysqli->set_charset('utf8'))
printf("[%d] %s\n", $mysqli->errno, $mysqli->errno);
$charsetInfo = $mysqli->get_charset();
if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
printf("[004] [%d] %s\n", $mysqli->errno, $mysqli->error);
@ -37,18 +42,16 @@ require_once('skipifconnectfailure.inc');
$tmp = $res->fetch_field();
var_dump($tmp);
if ($tmp->charsetnr != $charsets['results']['nr']) {
if ($tmp->charsetnr != $charsetInfo->number) {
printf("[005] Expecting charset %s/%d got %d\n",
$charsets['results']['charset'],
$charsets['results']['nr'], $tmp->charsetnr);
$charsetInfo->charset, $charsetInfo->number, $tmp->charsetnr);
}
if ($tmp->length != (1 * $charsets['results']['maxlen'])) {
if ($tmp->length != $charsetInfo->max_length) {
printf("[006] Expecting length %d got %d\n",
$charsets['results']['maxlen'],
$tmp->max_length);
$charsetInfo->max_length, $tmp->max_length);
}
if ($tmp->db != $db) {
printf("008] Expecting database '%s' got '%s'\n",
printf("[007] Expecting database '%s' got '%s'\n",
$db, $tmp->db);
}

View File

@ -21,7 +21,13 @@ require_once('skipifconnectfailure.inc');
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
require('table.inc');
$charsets = my_get_charsets($link);
// Make sure that client, connection and result charsets are all the
// same. Not sure whether this is strictly necessary.
if (!mysqli_set_charset($link, 'utf8'))
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
$charsetInfo = mysqli_get_charset($link);
if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 1")) {
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
@ -33,14 +39,14 @@ require_once('skipifconnectfailure.inc');
switch ($k) {
case 1:
/* label column, result set charset */
if ($field->charsetnr != $charsets['results']['nr']) {
if ($field->charsetnr != $charsetInfo->number) {
printf("[004] Expecting charset %s/%d got %d\n",
$charsets['results']['charset'],
$charsets['results']['nr'], $field->charsetnr);
$charsetInfo->charset,
$charsetInfo->number, $field->charsetnr);
}
if ($field->length != (1 * $charsets['results']['maxlen'])) {
if ($field->length != $charsetInfo->max_length) {
printf("[005] Expecting length %d got %d\n",
$charsets['results']['maxlen'],
$charsetInfo->max_length,
$field->max_length);
}
break;

View File

@ -66,7 +66,13 @@ require_once('skipifconnectfailure.inc');
printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
require('table.inc');
$charsets = my_get_charsets($link);
// Make sure that client, connection and result charsets are all the
// same. Not sure whether this is strictly necessary.
if (!mysqli_set_charset($link, 'utf8'))
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
$charsetInfo = mysqli_get_charset($link);
if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1", MYSQLI_USE_RESULT)) {
printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
@ -81,15 +87,13 @@ require_once('skipifconnectfailure.inc');
$field = mysqli_fetch_field($res);
var_dump($field);
/* label column, result set charset */
if ($field->charsetnr != $charsets['results']['nr']) {
if ($field->charsetnr != $charsetInfo->number) {
printf("[004] Expecting charset %s/%d got %d\n",
$charsets['results']['charset'],
$charsets['results']['nr'], $field->charsetnr);
$charsetInfo->charset, $charsetInfo->number, $field->charsetnr);
}
if ($field->length != (1 * $charsets['results']['maxlen'])) {
if ($field->length != $charsetInfo->max_length) {
printf("[005] Expecting length %d got %d\n",
$charsets['results']['maxlen'],
$field->max_length);
$charsetInfo->max_length, $field->max_length);
}
var_dump(mysqli_field_tell($res));
@ -217,7 +221,7 @@ bool(false)
Warning: mysqli_field_seek(): Invalid field offset in %s on line %d
bool(false)
bool(true)
object(stdClass)#3 (13) {
object(stdClass)#%d (13) {
[%u|b%"name"]=>
%unicode|string%(5) "_null"
[%u|b%"orgname"]=>

View File

@ -12,7 +12,13 @@ if (!function_exists('mysqli_stmt_get_result'))
--FILE--
<?php
require('table.inc');
$charsets = my_get_charsets($link);
// Make sure that client, connection and result charsets are all the
// same. Not sure whether this is strictly necessary.
if (!mysqli_set_charset($link, 'utf8'))
printf("[%d] %s\n", mysqli_errno($link), mysqli_errno($link));
$charsetInfo = mysqli_get_charset($link);
if (!($stmt = mysqli_stmt_init($link)) ||
!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'))
Label column, result set charset.
All of the following columns are "too hot" - too server dependent
*/
if ($field->charsetnr != $charsets['results']['nr']) {
if ($field->charsetnr != $charsetInfo->number) {
printf("[004] Expecting charset %s/%d got %d\n",
$charsets['results']['charset'],
$charsets['results']['nr'], $field->charsetnr);
$charsetInfo->charset,
$charsetInfo->number, $field->charsetnr);
}
if ($field->length != (1 * $charsets['results']['maxlen'])) {
if ($field->length != $charsetInfo->max_length) {
printf("[005] Expecting length %d got %d\n",
$charsets['results']['maxlen'],
$field->max_length);
$charsetInfo->max_length, $field->max_length);
}
}
}