如何从mysql表中获取自动递增字段名称或主键字段名称? [英] How can I get the auto incrementing field name or the primary key fieldname from a mysql table?
问题描述
在PHP中,如何在添加新rec时获得设置为自动递增的字段的字段名称?
In PHP, how do I get the field name of the field that's been set as to auto increment when a new rec is added to it?
在大多数情况下,它与表的PRIMARY_KEY相同,但不一定总是如此.
In most cases, it's the same as the PRIMARY_KEY of the table but not necessarily always.
因此,这个问题分为2部分,第二部分分支为第三部分.
So this question has 2 parts with the second one branching into a 3rd part.
1-如何获取自动递增字段名称的名称...
1- How to get the name of the auto-incrementing field name...
2-如何获取primary_key字段名称的名称...
2- How to get the name of the primary_key field name...
2.1当表使用多个字段作为主键时如何获取primary_key信息...
2.1 How to get the primary_key(s) info when a table uses more than one field as its primary key...
推荐答案
您可以使用SHOW COLUMNS FROM table
获取表信息.像这样:
You can get the table information using the SHOW COLUMNS FROM table
. Something like this:
$res = $mysqli->query('SHOW COLUMNS FROM tablename');
while($row = $res->fetch_assoc()) {
if ($row['Extra'] == 'auto_increment')
echo 'Field with auto_increment = '.$row['Field'];
if ($row['Key'] == 'PRI')
echo 'Field with primary key = '.$row['Field'];
}
这篇关于如何从mysql表中获取自动递增字段名称或主键字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!