]> granicus.if.org Git - php/commitdiff
Annother attempt at fixing the mysqli_fetch_field tests
authorNikita Popov <nikic@php.net>
Fri, 17 Aug 2012 13:17:29 +0000 (15:17 +0200)
committerNikita Popov <nikic@php.net>
Fri, 17 Aug 2012 17:47:59 +0000 (19:47 +0200)
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.

ext/mysqli/tests/connect.inc
ext/mysqli/tests/mysqli_fetch_field.phpt
ext/mysqli/tests/mysqli_fetch_field_oo.phpt
ext/mysqli/tests/mysqli_fetch_fields.phpt
ext/mysqli/tests/mysqli_field_seek.phpt
ext/mysqli/tests/mysqli_stmt_get_result_metadata_fetch_field.phpt

index 3a9d8ec258c8c2f369fd699da03e8fd68431a12b..4acc20cb9195c19136d4420cb05ad3a3a80838f5 100644 (file)
                        }
                }
 
-               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()) &&
index d1d358b342183f6b1a5f9bf0092897d9c112b7a1..2b9108072b4134861bf8bcb15da6396fa461b4d3 100644 (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,19 +40,17 @@ 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",
-                 $db, $tmp->db);
+                       $db, $tmp->db);
        }
 
        var_dump(mysqli_fetch_field($res));
@@ -174,4 +178,4 @@ object(stdClass)#%d (13) {
   [%u|b%"decimals"]=>
   int(0)
 }
-done!
\ No newline at end of file
+done!
index 2d5ad261b1315d4a3cf592701f9026f11e3af7e6..8c5609b163d2ef0b48c82ecea3180ab623d8a732 100644 (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);
        }
 
@@ -126,4 +129,4 @@ object(stdClass)#%d (13) {
 bool(false)
 
 Warning: mysqli_result::fetch_field(): Couldn't fetch mysqli_result in %s on line %d
-done!
\ No newline at end of file
+done!
index 479c71cbbc5a89caa8cc474928918b7c14ef98a8..6b66d6f231f33ca760a3573399f8f7514e73f183 100644 (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;
@@ -118,4 +124,4 @@ object(stdClass)#%d (13) {
 }
 
 Warning: mysqli_fetch_fields(): Couldn't fetch mysqli_result in %s on line %d
-done!
\ No newline at end of file
+done!
index a747bdfa019a3f3c31546aee283958e0c0404aea..449d2f90d41550af90970deae2a01ed697a8885c 100644 (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"]=>
@@ -248,4 +252,4 @@ object(stdClass)#3 (13) {
 
 Warning: mysqli_field_seek(): Couldn't fetch mysqli_result in %s on line %d
 NULL
-done!
\ No newline at end of file
+done!
index afaccaf3c7a538dc59eb2c8b2aa6b8559a1d2afc..739bf56ea150d799813000a5da69eb652806aad3 100644 (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);
                        }
                }
        }
@@ -173,4 +178,4 @@ object(stdClass)#%d (13) {
   [%u|b%"decimals"]=>
   int(31)
 }
-done!
\ No newline at end of file
+done!