<?php

// Taken from: <http://ca.php.net/manual/en/function.mysql-field-flags.php>
// Original by: amir at scrounch dot com
// Returns the name of the fields in the primary key for a table.
// Also keeps the fields in KEY order. Handy.
function getPrimaryKeyOf($table)
{ 
  $keys = Array();

  $query = sprintf("SHOW KEYS FROM `%s`", $table);
  $result = mysql_query($query) or die(mysql_error());

  while ($row = mysql_fetch_assoc($result))
  {
    if ( $row['Key_name'] == 'PRIMARY' )
    $keys[$row['Seq_in_index'] - 1] = $row['Column_name'];
  }// End while

  return $keys;
}// end function

// Returns a bunch of information about a table...
// The name of the auto-increment field, if any, fields in the
// primary key (using the function above), and all information
// about all fields.
function getTableInformationOf($table)
{
  $information = array(
    "auto" => "",
    "primary" => array(),
    "fields" => array()
  );

  $information['primary'] = getPrimaryKeyOf($table);
  $result = mysql_query("DESC `$table`");
  while ( $field = mysql_fetch_assoc($result) )
  {
    $information['fields'][] = $field;
    if ( $field['Extra'] == "auto_increment" )
      $information['auto'] = $field['Field'];
  }

  return $information;
}// End function

  $host  = "localhost";
  $user  = "root";
  $pass  = "";
  $dbase = "phpage";
  $table = "usuario";

  $con = mysql_connect( $host, $user )
    or die('Erro mysql_connect.');
  $db = mysql_select_db( $dbase, $con)
    or die('Erro select DB.');

  echo("\n <pre>");
  print_r(getTableInformationOf($table) );

?>

