How to get primary key of table?

To get the primary key of a table in PHP, you can use the SHOW KEYS SQL statement to retrieve information about the keys (including the primary key) of a table.

You can use the mysql_query() or mysqli_query() function to execute the SHOW KEYS statement and retrieve the result set.

Watch a course Learn object oriented PHP

Then you can use the mysql_fetch_array() or mysqli_fetch_array() function to iterate through the result set and check the Key_name field for the primary key.

Example:

<?php

$con = mysqli_connect("host", "username", "password", "database");
$result = mysqli_query($con, "SHOW KEYS FROM table_name");
while ($row = mysqli_fetch_array($result)) {
  if ($row['Key_name'] == 'PRIMARY') {
    echo $row['Column_name'];
  }
}

This will return the primary key column name of table_name.