How PHP Gets the field names and details of MySQL data tables

Source: Internet
Author: User

First we need to understand the SQL statements that query MySQL database/table related information:

The code is as follows:
SHOW DATABASES//Lists the MySQL Server database.
SHOW TABLES [from db_name]//Lists database data tables.
SHOW CREATE TABLES tbl_name//Export data table structure.
SHOW table status [from db_name]//Lists 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 full field from Tbl_name [from db_name]//list fields complete properties
SHOW index from Tbl_name [from db_name]//List table index.
SHOW Status//Lists the DB Server status.
SHOW VARIABLES//Lists the MySQL system environment variables.
SHOW processlist//Lists the execution commands.
SHOW GRANTS for user//List a user right

As can be seen from the SQL statements above, we can use show full columns to list fields and details, sample code:

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 ' <br/><br/> ';
Print_r ($row);
}

Printing results:

The code is as follows:
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] =&gt ; [7] = = select,insert,update,references [Privileges] = select,insert,update,references [8] = = [Comment] = =)

Array ([0] = title [Field] = title [1] = = char (+) [Type] + char [2] = [Collation] = = Utf8_general_ci [3] = = [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, etc.)

Array ([0] = des [Field] = des [1] = = varchar (255) [Type] = = varchar (255) [2] = = Utf8_general_ci [Collat ION] = Utf8_general_ci [3] = = [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 list the fields in MySQL results by mysql_list_fields-. Mysql_list_fields () Gets the information for the given table name, which is the database name and table name, and returns a result pointer.

However, the Mysql_list_fields () function is obsolete. It is best to use mysql_query () to emit an SQL statement that SHOW COLUMNS from table [like ' name '] instead. For more information, refer to the PHP Help documentation: Php:mysql_list_fields-manua

How PHP Gets the field names and details of MySQL data tables

Related Article

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.