Thomas
Thomas

Reputation: 4719

PHP PDO get auto_increment flag

Is there a way to check whether a column has been specified as auto_increment? I know that the mysqli extension supports that. However PDO's getColumnMeta() method does not provide this info

Is there any workaround available?

Thanks

Upvotes: 1

Views: 298

Answers (2)

Code Lღver
Code Lღver

Reputation: 15593

Use the cubrid_schema function of PDO to know the description of table and its column.

OR

You can use the below code:

$q = $dbh->prepare("DESC tablename");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);

Reference site.

Upvotes: 1

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

You can query it from information schema like:

SELECT 
  EXTRA 
FROM 
  information_schema.columns 
WHERE 
  table_name = 'your_table_name' AND COLUMN_NAME = 'your_primary_key' 
  AND table_schema = DATABASE();

And you will get auto_increment.

The Extra field contains any additional information that is available about a given column. The value is auto_increment for columns that have the AUTO_INCREMENT attribute and empty otherwise.

INFORMATION_SCHEMA Tables

Upvotes: 1

Related Questions