PHP gets the MySQL data table field name and details method, MySQL field _php tutorial

Source: Internet
Author: User

PHP gets the MySQL data table field name and details method, MySQL field


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

Copy the Code code 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:
Copy the Code code 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);
}

Printing results:
Copy the Code code 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] => ; [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 to query MySQL database in PHP code can get the ID number of the largest record in a field in a data table: see the problem Supplement

I was doing this test:
CREATE TABLE Music (
ID varchar (10),
Title varchar (100),
Name varchar (10)
);
Insert into music values (' 2 ', ' parting ', ' 12354 ');
Insert into music values (' 15 ', ' Friends ', ' 5454 ');
Insert into music values (' 161 ', ' Farewell ', ' 4668 ');

$conn =mysql_connect ("localhost:3307", "Database user name", "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 varchar in MySQL is not able to use Max () so it solves this problem by converting the ID type by id+0, you can use Max () directly if you are building a table with an integer ID. Detailed explanation see: hb.qq.com/a/20110624/000061.htm.

PHP gets all the data table information in the MySQL database

mysql_query Execute SQL statements, show tables these

http://www.bkjia.com/PHPjc/885652.html www.bkjia.com true http://www.bkjia.com/PHPjc/885652.html techarticle PHP gets the MySQL data table field name and details of the method, MySQL field first we need to understand the query MySQL database/table related information SQL statement: Copy code code as follows ...

  • 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.