PHP Method for Getting Field Names and Details of mysql Datasheet

  • 2021-07-21 07:39:42
  • OfStack

First, we need to understand the SQL statement for querying information about MySQL database/table:


SHOW DATABASES                                // List MySQL Server Database.
SHOW TABLES [FROM db_name]                    // Lists database tables.
SHOW CREATE TABLES tbl_name                    // Export the data table structure.
SHOW TABLE STATUS [FROM db_name]              // Lists data tables and table status information.
SHOW COLUMNS FROM tbl_name [FROM db_name]     // List table fields
SHOW FIELDS FROM tbl_name [FROM db_name] , DESCRIBE tbl_name [col_name] .
SHOW FULL COLUMNS FROM tbl_name [FROM db_name]// List Fields and Details
SHOW FULL FIELDS FROM tbl_name [FROM db_name] // List the complete properties of the field
SHOW INDEX FROM tbl_name [FROM db_name]       // List table indexes.
SHOW STATUS                                  // List DB Server Status.
SHOW VARIABLES                               // List MySQL System environment variables.
SHOW PROCESSLIST                             // List the execution commands.
SHOW GRANTS FOR user                         // List the permissions of a user

As you can see from the above SQL statement, we can use SHOW FULL COLUMNS to list the fields and details, sample code:


$rescolumns = mysql_query("SHOW FULL COLUMNS FROM ".TB_NAME."") ;
while($row = mysql_fetch_array($rescolumns)){
//  echo ' Field name: '.$row['Field'].'- Data type: '.$row['Type'].'- Note: '.$row['Comment'];
//  echo '<br/><br/>';
  print_r($row);
}

Print results:


Array ( [0] => id [Field] => id [1] => char(2) [Type] => char(2) [2] => utf8_general_ci [Collation] => utf8_general_ci [3] => NO [Null] => NO [4] => PRI [Key] => PRI [5] => [Default] => [6] => [Extra] => [7] => select,insert,update,references [Privileges] => select,insert,update,references [8] => [Comment] => ) Array ( [0] => title [Field] => title [1] => char(50) [Type] => char(50) [2] => utf8_general_ci [Collation] => utf8_general_ci [3] => YES [Null] => YES [4] => [Key] => [5] => [Default] => [6] => [Extra] => [7] => select,insert,update,references [Privileges] => select,insert,update,references [8] => Suggested storage: title, name and other information [Comment] => Suggested storage: title, name and other information ) Array ( [0] => des [Field] => des [1] => varchar(255) [Type] => varchar(255) [2] => utf8_general_ci [Collation] => utf8_general_ci [3] => YES [Null] => YES [4] => [Key] => [5] => [Default] => [6] => [Extra] => [7] => select,insert,update,references [Privileges] => select,insert,update,references [8] => [Comment] => ) ......

Supplementary explanatory information:

Of course, you can also list the fields in the MySQL result through mysql_list_fields. mysql_list_fields () takes information for a given table name, with database name and table name as parameters, and returns a result pointer.

However, the mysql_list_fields () function is obsolete. Instead, use mysql_query () to issue an SQL statement of SHOW COLUMNS FROM table [LIKE 'name']. Refer to the PHP Help Documentation for details: PHP: mysql_list_fields-Manua


Related articles: