<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "sakila");
/* 检查连接 */
if (mysqli_connect_errno()) {
printf("连接失败: %s\n", mysqli_connect_error());
exit();
}
foreach (array('latin1', 'utf8') as $charset) {
// 设置字符集,以显示它对某些值的影响(例如,以字节为单位的长度)
mysqli_set_charset($link, $charset);
$query = "SELECT actor_id, last_name from actor ORDER BY actor_id";
echo "======================\n";
echo "字符集: $charset\n";
echo "======================\n";
if ($result = mysqli_query($link, $query)) {
/* 获取所有列的字段信息 */
$finfo = mysqli_fetch_fields($result);
foreach ($finfo as $val) {
printf("名称: %s\n", $val->name);
printf("表: %s\n", $val->table);
printf("最大长度: %d\n", $val->max_length);
printf("长度: %d\n", $val->length);
printf("charsetnr: %d\n", $val->charsetnr);
printf("标识: %d\n", $val->flags);
printf("类型: %d\n\n", $val->type);
}
mysqli_free_result($result);
}
}
mysqli_close($link);
?>
======================
Character Set: latin1
======================
Name: actor_id
Table: actor
Max. Len: 3
Length: 5
charsetnr: 63
Flags: 49699
Type: 2
Name: last_name
Table: actor
Max. Len: 12
Length: 45
charsetnr: 8
Flags: 20489
Type: 253
======================
Character Set: utf8
======================
Name: actor_id
Table: actor
Max. Len: 3
Length: 5
charsetnr: 63
Flags: 49699
Type: 2
Name: last_name
Table: actor
Max. Len: 12
Length: 135
charsetnr: 33
Flags: 20489