PHP/MySQL Database/Table MetaData

Материал из Web эксперт
Перейти к: навигация, поиск

Get column data type

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT id as Employee_ID FROM Employee";
   $result = mysql_query($query);
   $row = mysql_fetch_row($result);
   echo mysql_field_type($result, 0);
?>



Get column name, type and max length

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT * FROM Employee LIMIT 1";
   $result = mysql_query($query);
   $fields = mysql_num_fields($result);
   for($count=0;$count<$fields;$count++) {
      $field = mysql_fetch_field($result,$count);
      echo "<p>$field->name $field->type ($field->max_length)</p>";
   }
?>



Get field length

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT FirstName FROM Employee ";
   $result = mysql_query($query);
   $row = mysql_fetch_row($result);
   echo mysql_field_len($result, 0);
?>



Get table column flag

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");
   $query = "SELECT id as Employee_ID FROM Employee";
   $result = mysql_query($query);
   $row = mysql_fetch_row($result);
   echo mysql_field_flags($result, 0);
?>



Total number of fields in a table

<?php
   mysql_connect("mysql153.secureserver.net","wbex","password");
   mysql_select_db("wbex");// databasename
   $fields = mysql_list_fields("wbex","Employee");// databasename and tablename
   echo "Total number of fields returned: ".mysql_num_fields($fields).".<br />";
?>