if ($flags & MYSQLI_GROUP_FLAG) { $ret .= 'MYSQLI_GROUP_FLAG '; } return $ret; } require_once "connect.inc"; $tmp = NULL; $link = NULL; $test_table_name = 'test_mysqli_field_seek_table_1'; require 'table.inc'; // 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_mysqli_field_seek_table_1 ORDER BY id LIMIT 1", MYSQLI_USE_RESULT))) { printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } var_dump(mysqli_field_seek($res, -1)); var_dump(mysqli_fetch_field($res)); var_dump(mysqli_field_seek($res, 0)); var_dump(mysqli_fetch_field($res)); var_dump(mysqli_field_seek($res, 1)); $field = mysqli_fetch_field($res); var_dump($field); /* label column, result set charset */ if ($field->charsetnr != $charsetInfo->number) { printf("[004] Expecting charset %s/%d got %d\n", $charsetInfo->charset, $charsetInfo->number, $field->charsetnr); } if ($field->length != $charsetInfo->max_length) {
$tmp = mysqli_fetch_assoc($res); mysqli_free_result($res); if (!($character_sets_dir = $tmp['Value'])) { printf("[014] Cannot fetch character_sets_dir, test will fail\n"); } if (!is_object($charset = mysqli_get_charset($link))) { printf("[015] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset); } if (!isset($charset->charset) || !in_array(gettype($charset->charset), array("string", "unicode")) || $character_set_connection !== $charset->charset) { printf("[016] Expecting string/%s, got %s/%s\n", $character_set_connection, gettype($charset->charset), $charset->charset); } if (!isset($charset->collation) || !in_array(gettype($charset->collation), array("string", "unicode")) || $collation_connection !== $charset->collation) { printf("[017] Expecting string/%s, got %s/%s\n", $collation_connection, gettype($charset->collation), $charset->collation); } if (!isset($charset->dir) || !is_string($charset->dir)) { printf("[019] Expecting string - ideally %s*, got %s/%s\n", $character_sets_dir, gettype($charset->dir), $charset->dir); } if (!isset($charset->min_length) || !is_int($charset->min_length) || $charset->min_length < 0 || $charset->min_length > $charset->max_length) { printf("[020] Expecting int between 0 ... %d, got %s/%s\n", $charset->max_length, gettype($charset->min_length), $charset->min_length); } if (!isset($charset->number) || !is_int($charset->number) || $charset->number !== (int) $id) { printf("[021] Expecting int/%d, got %s/%s\n", $id, gettype($charset->number), $charset->number); } if (!isset($charset->state) || !is_int($charset->state)) { printf("[022] Expecting int/any, got %s/%s\n", gettype($charset->state), $charset->state); } mysqli_close($link); if (NULL !== ($tmp = mysqli_get_charset($link))) { printf("[023] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp); } print "done!";
/** * 设置字符编码 * * @param {String} $encoding * @return {String} */ public static function charset($encoding = '') { if (!empty($encoding)) { mysqli_set_charset(SQL::$connection, $encoding); DEBUG::put('charset=' . $encoding, 'MySQL'); } return mysqli_get_charset(SQL::$connection); }
printf("[011] Charsets/collations have not been reset.\n"); printf("Got:\n"); var_dump($new); printf("Expected:\n"); var_dump($defaults); } if (version_compare(PHP_VERSION, '5.9.9', '>') == 1) { // charsets cannot take any other value but utf8 in unicode mode $defaults['charset_client'] = 'utf8'; $defaults['charset_connection'] = 'utf8'; $defaults['charset_results'] = 'utf8'; $defaults['collation_connection'] = 'utf8_general_ci'; } if ($new != $defaults) { printf("[012] Charsets/collations have not been reset to their defaults.\n"); printf("Got:\n"); var_dump($new); printf("Expected:\n"); var_dump($defaults); } if (!is_object($charset = mysqli_get_charset($link))) { printf("[013] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset); } if ($charset->charset != $defaults['charset_connection']) { printf("[014] Expecting connection charset to be %s got %s\n", $defaults['charset_connection'], $charset->charset); } if ($charset->collation != $defaults['collation_connection']) { printf("[015] Expecting collation to be %s got %s\n", $defaults['collation_connection'], $charset->collation); } mysqli_close($link); print "done!";
public function mysqldump($db, $filename) { $link = $this->connect($db); WPAdm_Core::log("MySQL of Dump was started"); $tables = array(); if (!($result = mysqli_query($link, 'SHOW TABLES'))) { $this->setError(mysqli_error($link)); } while ($row = mysqli_fetch_row($result)) { $tables[] = $row[0]; } //cycle through $return = ''; $charset = mysqli_get_charset($link); if (isset($charset->charset)) { $return .= "SET NAMES '{$charset->charset}';\n\n"; WPAdm_Core::log("SET NAMES Database {$charset->charset};"); } foreach ($tables as $table) { WPAdm_Core::log("Add a table {$table} in the database dump"); mysqli_close($link); $link = $this->connect($db); if (!($result = mysqli_query($link, 'SELECT * FROM ' . $table))) { $this->setError(mysqli_error($link)); } $num_fields = mysqli_num_fields($result); $return .= 'DROP TABLE ' . $table . ';'; if (!($ress = mysqli_query($link, 'SHOW CREATE TABLE ' . $table))) { $this->setError(mysqli_error($link)); } $row2 = mysqli_fetch_row($ress); $return .= "\n\n" . $row2[1] . ";\n\n"; for ($i = 0; $i < $num_fields; $i++) { while ($row = mysqli_fetch_row($result)) { $return .= 'INSERT INTO ' . $table . ' VALUES('; for ($j = 0; $j < $num_fields; $j++) { //$row[$j] = mb_convert_encoding($row[$j], 'UTF-8', 'auto'); $row[$j] = addslashes($row[$j]); $row[$j] = str_replace("\n", "\\n", $row[$j]); if (isset($row[$j])) { $return .= '"' . $row[$j] . '"'; } else { $return .= '""'; } if ($j < $num_fields - 1) { $return .= ','; } } $return .= ");\n"; } } $return .= "\n\n\n"; } mysqli_close($link); $handle = fopen($filename, 'w+'); fwrite($handle, $return); fclose($handle); WPAdm_Core::log("MySQL of Dump was finished"); return true; }
<?php $host = getenv("MYSQL_TEST_HOST") ? getenv("MYSQL_TEST_HOST") : "localhost"; $port = getenv("MYSQL_TEST_PORT") ? getenv("MYSQL_TEST_PORT") : 3306; $user = getenv("MYSQL_TEST_USER") ? getenv("MYSQL_TEST_USER") : "root"; $passwd = getenv("MYSQL_TEST_PASSWD") ? getenv("MYSQL_TEST_PASSWD") : ""; $db = getenv("MYSQL_TEST_DB") ? getenv("MYSQL_TEST_DB") : "test"; $mysqli = new mysqli($host, $user, $passwd, $db, $port); $mysqli->set_charset("utf8"); var_dump($mysqli->get_charset()->charset); var_dump(mysqli_get_charset($mysqli)->charset);