How to get primary key of table?

前端 未结 14 2116
不思量自难忘°
不思量自难忘° 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:54

    I've got it, finally!

    <?php
    
    function mysql_get_prim_key($table){
    $sql = "SHOW INDEX FROM $table WHERE Key_name = 'PRIMARY'";
    $gp = mysql_query($sql);
    $cgp = mysql_num_rows($gp);
    if($cgp > 0){
    // Note I'm not using a while loop because I never use more than one prim key column
    $agp = mysql_fetch_array($gp);
    extract($agp);
    return($Column_name);
    }else{
    return(false);
    }
    }
    
    ?>
    
    0 讨论(0)
  • 2020-12-02 15:55

    If you have spatial tables in your database, use:

    SHOW KEYS FROM table WHERE Key_name = 'PRIMARY' OR Key_name = 'OGR_FID'
    
    0 讨论(0)
提交回复
热议问题