PHP method for obtaining the field name and detailed information of the mysql DATA table, mysql Field

Source: Internet
Author: User

PHP method for obtaining the field name and detailed information of the mysql DATA table, mysql Field

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

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

Print result:
Copy codeThe 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: PHP: mysql_list_fields-Manua


In PHP code, how can I query the MYSQL database to obtain the ID number of the largest record in a field in the data table? For example: See Question supplement.

I did the following test:
Create table music (
Id varchar (10 ),
Title varchar (100 ),
Name varchar (10)
);
Insert into music values ('2', 'parting ', '123 ');
Insert into music values ('15', 'friends', '123 ');
Insert into music values ('20160301', 'farewell ', '20160301 ');

<? Php
$ Conn = mysql_connect ("localhost: 3307", "database username", "Database Password ");
Mysql_select_db ("date ");
$ Result = mysql_query ("select max (id + 0) max_id from music", $ conn );
$ Field = mysql_fetch_row ($ result );
Print_r ($ field );
?>
Result: Array ([0] => 161)
Because mysql varchar does not use max (), id + 0 is used to convert the id type to solve this problem. If the id is an integer type during table creation, you can directly use max (). For more information, see hb.qq.com/a/20151124/41061.htm.

Php obtains information about all data tables in the mysql database.

Mysql_query: Execute SQL statements, such as show tables.

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.