我无法正确编码整个MYSQLI对象。全部返回NULL。我尝试了很多方法来转换编码字符,但是没有运气。
$sql = "SELECT * FROM t_user";
$char = $mysqli->set_charset('utf8mb4');
var_dump($char);
$mysqli->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
$mysqli->set_charset('utf8mb4');
//Source: https://gist.github.com/oscar-broman/3653399
utf8_encode_deep($mysqli);
$result = $mysqli->query($sql);
var_dump($mysqli);
var_dump(get_object_vars($mysqli));
echo "\n\n\n".json_encode($mysqli);
echo "\n\n\n".$result->field_count;
echo("\n\n\n".json_last_error_msg());
函数复制自: https://gist.github.com/oscar-broman/3653399
function utf8_encode_deep(&$input) {
if (is_string($input)) {
$input = utf8_encode($input);
} else if (is_array($input)) {
foreach ($input as &$value) {
utf8_encode_deep($value);
}
unset($value);
} else if (is_object($input)) {
$vars = array_keys(get_object_vars($input));
foreach ($vars as $var) {
utf8_encode_deep($input->$var);
}
}
}
var_dump($ char);
D:\xampp\htdocs\mb\action.php:636:
bool(true)
var_dump($ mysqli);
D:\xampp\htdocs\mb\action.php:644:
class mysqli#1 (19) {
public $affected_rows =>
int(24)
public $client_info =>
string(79) "mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $"
public $client_version =>
int(50012)
public $connect_errno =>
int(0)
public $connect_error =>
NULL
public $errno =>
int(0)
public $error =>
string(0) ""
public $error_list =>
array(0) {
}
public $field_count =>
int(9)
public $host_info =>
string(20) "localhost via TCP/IP"
public $info =>
NULL
public $insert_id =>
int(0)
public $server_info =>
string(21) "5.5.5-10.1.38-MariaDB"
public $server_version =>
int(50505)
public $stat =>
string(135) "Uptime: 72296 Threads: 1 Questions: 5062 Slow queries: 0 Opens: 64 Flush tables: 1 Open tables: 51 Queries per second avg: 0.070"
public $sqlstate =>
string(5) "00000"
public $protocol_version =>
int(10)
public $thread_id =>
int(643)
public $warning_count =>
int(0)
}
var_dump(get_object_vars($ mysqli));
D:\xampp\htdocs\mb\action.php:646:
array(19) {
'affected_rows' =>
NULL
'client_info' =>
NULL
'client_version' =>
NULL
'connect_errno' =>
NULL
'connect_error' =>
NULL
'errno' =>
NULL
'error' =>
NULL
'error_list' =>
NULL
'field_count' =>
NULL
'host_info' =>
NULL
'info' =>
NULL
'insert_id' =>
NULL
'server_info' =>
NULL
'server_version' =>
NULL
'stat' =>
NULL
'sqlstate' =>
NULL
'protocol_version' =>
NULL
'thread_id' =>
NULL
'warning_count' =>
NULL
}
json_encode($ mysqli);
{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null}
$ result-> field_count;
9
json_last_error_msg()
No error