如何使用 mysqli_fetch_fields 获取 auto_increment 详情?
How to get auto_increment particulars using mysqli_fetch_fields?
objectice: 使用 mysqli_fetch_fields
获取字段的 auto_increment 细节
代码:
<?php
require_once("dbc.php");
$query="SELECT id, name, age from student WHERE 1>2";
$result=mysqli_query($dbc, $query);
$metas=mysqli_fetch_fields($result);
foreach($metas as meta){
$col_name=meta->name;
$col_type=meta->type;
$col_length=meta->length;
$col_flags=meta->flags;
echo "col_name: $col_name, col_type: $col_type, col_length: $col_length, col_flags: $col_flags";
}
mysqli_close($dbc);
?>
观察:
在 mysql 提示中
mysql> describe student;
Field Type Null Key Default Extra
id int(16) NO PRI NULL auto_increment
但在 mysqli
col_name: id, col_type: 3 , col_length: 11, col_flags: 49967
col_flags值49967对应主键
请指导我使用 mysqli_fetch_fields 获取字段是否自动递增?
用于测试 AUTO_INCREMENT
的适当标志位是位 9 (AUTO_INCREMENT_FLAG
) (Source)。在 PHP 你只需要添加 MYSQLI_
前缀。像这样:
$auto_increment = $col_flags & MYSQLI_AUTO_INCREMENT_FLAG;
objectice: 使用 mysqli_fetch_fields
获取字段的 auto_increment 细节代码:
<?php
require_once("dbc.php");
$query="SELECT id, name, age from student WHERE 1>2";
$result=mysqli_query($dbc, $query);
$metas=mysqli_fetch_fields($result);
foreach($metas as meta){
$col_name=meta->name;
$col_type=meta->type;
$col_length=meta->length;
$col_flags=meta->flags;
echo "col_name: $col_name, col_type: $col_type, col_length: $col_length, col_flags: $col_flags";
}
mysqli_close($dbc);
?>
观察:
在 mysql 提示中
mysql> describe student;
Field Type Null Key Default Extra
id int(16) NO PRI NULL auto_increment
但在 mysqli
col_name: id, col_type: 3 , col_length: 11, col_flags: 49967
col_flags值49967对应主键
请指导我使用 mysqli_fetch_fields 获取字段是否自动递增?
用于测试 AUTO_INCREMENT
的适当标志位是位 9 (AUTO_INCREMENT_FLAG
) (Source)。在 PHP 你只需要添加 MYSQLI_
前缀。像这样:
$auto_increment = $col_flags & MYSQLI_AUTO_INCREMENT_FLAG;