Can I return the default values ​​for column names in a MySQL table?

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

Here is what I use to get table 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 also trying to get the default data value for each column name.

+4
source share
3 answers

Try the following:

 $query = "SHOW FULL COLUMNS FROM tableName"; // ... 

Column Default .

Hope I helped.

+4
source

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

 $query = <<< endsql SELECT Column_Default FROM Information_Schema.Columns WHERE Table_Schema = '$yourSchema' AND Table_Name = '$yourTableName' AND Column_Name = '$yourColumnName' endsql; 
+4
source

Just enter the index after FETCH_COLUMN. I have not tried your code, but based on the DESCRIBE manual, it will return 6 description columns. Therefore, if you read this , you can get a solution. I read them, and I think that simply adding the 4th index to my code as shown below will be your solution.

 $table_fields = $q->fetchAll(PDO::FETCH_COLUMN,4); 
-1
source

All Articles