Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get back default values for column names in a MySQL table? [duplicate]

Tags:

php

mysql

pdo

I can get the column names for a table but is there a way to retrieve the default data value for each column?

Here is what I'm using to get the tables column names:

$q = $dbh->query("DESCRIBE tablename"); 
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);

print_r($table_fields);

This prints an array of column names, but I'm trying to get the default data value for each column name also.

like image 597
Paul Avatar asked Apr 10 '13 01:04

Paul


1 Answers

Another option is to go to the data dictionary and find the value in Information_Schema.Columns. This allows you to limit results to a single column.

$query = <<< endsql
 SELECT Column_Default
  FROM Information_Schema.Columns
  WHERE Table_Schema = '$yourSchema'
    AND Table_Name = '$yourTableName'
    AND Column_Name = '$yourColumnName'
endsql;
like image 79
Ed Gibbs Avatar answered Sep 30 '22 17:09

Ed Gibbs