Examples of php getting MySQL field names and other information

Source: Internet
Author: User

First we need to know the following SQL statement to query MySQL database/table related information:

The code is as follows Copy Code

Show DATABASES//list MySQL Server database.
Show table [from db_name]//list database datasheet.
Show CREATE tables Tbl_name//export datasheet structure.
Show table status [from db_name]//List data tables and table state 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 fully FIELDS from Tbl_name [from db_name]//list field full properties
Show the index from Tbl_name [from db_name]//Lists table indexes.
Show status//list DB Server status.
Show VARIABLES//list MySQL system environment variables.
Show processlist//list execution commands.
Show grants for user//List a user right

As you can see from the above SQL statement, we can use show full columns to list fields and details, sample code:

The code is as follows Copy Code
$rescolumns = mysql_query ("Show All COLUMNS from". Tb_name. "") ;
while ($row = Mysql_fetch_array ($rescolumns)) {
echo ' Field name: '. $row [' field ']. ' -Data type: '. $row [' type ']. ' -Note: '. $row [' Comment '];
Echo ' <br/> www.111cn.net <br/> ';
Print_r ($row);
}

Print results:

The code is as follows Copy Code

Array ([0] => ID [Field] => ID [1] => char (2) [Type] => char (2) [2] => utf8_general_ci [collation] => u TF8_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 (m) [Type] => char (2) => utf8_general_ci [collation] => utf8_general_ci [3] => Yes [Null] => Yes [4] => [Key] => [5] => [Default] => [6] => [Extra] =& Gt [7] => select,insert,update,references [privileges] => select,insert,update,references [8] => recommended storage: Title, name and other information [ Comment] => Recommended storage: Title, name and other information)

Array ([0] => des [Field] => des [1] => varchar (255) [Type] => varchar (255) [2] => utf8_general_ci [Collat ION] => 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] =>)

............

Additional explanatory information:

Of course you can also list the fields in the MySQL results by mysql_list_fields-. Mysql_list_fields () Gets the information for the given table name, the parameter is the database name and the table name, and returns a result pointer.
However, the Mysql_list_fields () function is obsolete. It is best to use mysql_query () to emit a "show COLUMNS" from table [like ' name '] in place of an SQL statement. Detailed reference to the PHP Help document: Php:mysql_list_fields-manua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.