14 December 2011

How to get MySQL columns data of a table in PHP. The data includes name, type, null allowed or not, key, default value and extra for auto_increase and such.

Source code viewer
  1. $schema = array();
  2. $host = 'localhost';
  3. $username = 'root';
  4. $password = '';
  5. $link = mysql_connect($host, $username, $password);
  6. if ($link) {
  7. mysql_select_db('database_name', $link);
  8. $result = mysql_query('SHOW FIELDS FROM '.mysql_real_escape_string('table_name'), $link);
  9. while ($row = mysql_fetch_row($result)) {
  10. $schema[] = $row;
  11. }
  12. mysql_close($link);
  13. }
  14. print_r($schema);
  15.  
Programming Language: PHP