mysqli_result::fetch_fields
mysqli_result::fetch_fields
mysqli_fetch_fields
(PHP 5, PHP 7)
mysqli_result :: fetch_fields -- mysqli_fetch_fields — 返回表示结果集中的字段的对象数组
Description
面向对象的风格
array mysqli_result::fetch_fields ( void )
程序风格
array mysqli_fetch_fields ( mysqli_result $result )
这个函数与mysqli_fetch_field()函数具有相同的用途,唯一区别在于,不是一次为每个字段返回一个对象,而是以对象数组的形式返回列。
参数
`result`
仅过程风格:由mysqli_query(),mysqli_store_result()或mysqli_use_result()返回的结果集标识符。
返回值
返回包含字段定义信息的对象数组,或者FALSE
没有字段信息可用。
属性 | 描述 |
---|---|
name | 列的名称 |
ORGNAME | 如果指定了别名,则为原始列名称 |
表 | 该字段所属表的名称(如果未计算) |
orgtable | 原始表名,如果指定了别名 |
最长长度 | 结果集字段的最大宽度。 |
长度 | 字段的宽度,以字节为单位,如表定义中所指定。请注意,此数字(字节)可能与您的表定义值(字符)有所不同,具体取决于您使用的字符集。例如,字符集utf8每个字符有3个字节,所以varchar(10)将返回utf8(10 * 3)的长度为30,但为latin1(10 * 1)返回10。 |
的charsetnr | 字段的字符集编号(id)。 |
旗 | 表示字段的位标志的整数。 |
类型 | 用于此字段的数据类型 |
小数点 | 使用的小数位数(用于整数字段) |
例子
Example#1面向对象的风格
<?php
$mysqli = new mysqli("127.0.0.1", "root", "foofoo", "sakila"
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error
exit(
}
foreach (array('latin1', 'utf8') as $charset) {
// Set character set, to show its impact on some values (e.g., length in bytes)
$mysqli->set_charset($charset
$query = "SELECT actor_id, last_name from actor ORDER BY actor_id";
echo "======================\n";
echo "Character Set: $charset\n";
echo "======================\n";
if ($result = $mysqli->query($query)) {
/* Get field information for all columns */
$finfo = $result->fetch_fields(
foreach ($finfo as $val) {
printf("Name: %s\n", $val->name
printf("Table: %s\n", $val->table
printf("Max. Len: %d\n", $val->max_length
printf("Length: %d\n", $val->length
printf("charsetnr: %d\n", $val->charsetnr
printf("Flags: %d\n", $val->flags
printf("Type: %d\n\n", $val->type
}
$result->free(
}
}
$mysqli->close(
?>
示例#2程序风格
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "sakila"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
foreach (array('latin1', 'utf8') as $charset) {
// Set character set, to show its impact on some values (e.g., length in bytes)
mysqli_set_charset($link, $charset
$query = "SELECT actor_id, last_name from actor ORDER BY actor_id";
echo "======================\n";
echo "Character Set: $charset\n";
echo "======================\n";
if ($result = mysqli_query($link, $query)) {
/* Get field information for all columns */
$finfo = mysqli_fetch_fields($result
foreach ($finfo as $val) {
printf("Name: %s\n", $val->name
printf("Table: %s\n", $val->table
printf("Max. Len: %d\n", $val->max_length
printf("Length: %d\n", $val->length
printf("charsetnr: %d\n", $val->charsetnr
printf("Flags: %d\n", $val->flags
printf("Type: %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