Example of php obtaining mysql field names and other information

Source: Internet
Author: User
This article describes how to use php to obtain mysql field names and other information. For more information, see

This article describes how to use php to obtain mysql field names and other information. For more information, see

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

The Code is as follows:


Show databases // list MySQL Server DATABASES.
Show tables [FROM db_name] // lists database data 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 data 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 attributes of a field
Show index from tbl_name [FROM db_name] // List table indexes.
Show status // list the STATUS of the DB Server.
Show variables // list MySQL system environment VARIABLES.
Show processlist // list the execution commands.
Show grants for user // list permissions of a user


From the preceding SQL statement, we can use SHOW FULL COLUMNS to list fields and details. The sample code is as follows:

The Code is as follows:


$ 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'

';
Print_r ($ row );
}

Print result:

The Code is as follows:

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] => 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 [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] =>)

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


Additional information:
Of course, you can also use mysql_list_fields-to list fields in MySQL results. Mysql_list_fields () obtains the information of the given table name. The parameter is the database name and table name, and a result pointer is returned.
However, the mysql_list_fields () function is out of date. It is better to use mysql_query () to issue an SQL statement of SHOW COLUMNS FROM table [LIKE 'name. For details, refer to the PHP help documentation:

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.