How to get primary key of table?

前端 未结 14 2304
不思量自难忘°
不思量自难忘° 2020-12-02 15:01

Is there a way to get the name of primary key field from mysql-database? For example:

I have a table like this:

+----+------+
| id | name |
+----+---         


        
14条回答
  •  误落风尘
    2020-12-02 15:42

    Shortest possible code seems to be something like

    // $dblink contain database login details 
    // $tblName the current table name 
    $r = mysqli_fetch_assoc(mysqli_query($dblink, "SHOW KEYS FROM $tblName WHERE Key_name = 'PRIMARY'")); 
    $iColName = $r['Column_name']; 
    

提交回复
热议问题