如何使用mysqli_fetch_fields获取auto_increment详细信息?

时间:2019-10-14 03:36:08

标签: php mysqli auto-increment

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获取字段是否自动递增?

1 个答案:

答案 0 :(得分:1)

要测试AUTO_INCREMENT的适当标志位是第9位(AUTO_INCREMENT_FLAG)(Source)。在PHP中,您只需要添加MYSQLI_前缀。像这样:

$auto_increment = $col_flags & MYSQLI_AUTO_INCREMENT_FLAG;