PHP MySQL three ways to get the name and field information of a table _php tips

Source: Internet
Author: User

PHP MySQL three ways to get table field names and fields information

First give information about the tables used in this example:

Use DESC to get table field information

The PHP code is as follows:

<?php 
  mysql_connect ("localhost", "root", "");
  mysql_select_db ("test");
  $query = "desc student";
  $result = mysql_query ($query);
  while ($row =mysql_fetch_assoc ($result)) {
 print_r ($row);
  }
? >

Run Result:

Array
(
  [Field] => student_id
  [Type] => int (4)
  [Null] => NO
  [Key] => PRI
  [Default] => 
  [Extra] => auto_increment
)
Array
(
  [Field] => student_name
  [Type] => varchar (m)
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
)
Array
(
  [Field] => class_id
  [Type] => int (4)
  [Null] => NO
  [Key] => 
  [Default] => 
  [Extra] => 
)
Array
(
  [Field] => total_score
  [Type] => int (4)
  [Null] => NO
  [Key] => 
  [Default] = > 
  [Extra] => 
)
 

Get table field information using Show full fields

The PHP code is as follows:

<?php 
  mysql_connect ("localhost", "root", "");
  mysql_select_db ("test");
  $query = "Show full COLUMNS from student";
  $result = mysql_query ($query);
  while ($row =mysql_fetch_assoc ($result)) {
 print_r ($row);
  }
? >

Run Result:

 Array ([Field] => student_id [Type] => Int (4) [collation] => [Null] =& Gt NO [Key] => PRI [Default] => [Extra] => auto_increment [privileges] => select,insert,update,reference s [Comment] => Array ([Field] => student_name [Type] => varchar (m) [collation] => Latin1_swedish_ CI [Null] => NO [Key] => [Default] => [Extra] => [privileges] => Select,insert,update,referenc es [Comment] => Array ([Field] => class_id [Type] => Int (4) [collation] => [Null] => NO [K EY] => [Default] => [Extra] => [privileges] => select,insert,update,references [Comment] =>) A  Rray ([Field] => total_score [Type] => Int (4) [collation] => [Null] => NO [Key] => [Default] => [Extra] => [privileges] => select,insert,update,references [Comment] =>) 

Get table field information by using the Mysql_fetch_field method

The PHP code is as follows:

<?php
  mysql_connect ("localhost", "root", "");
  mysql_select_db ("test");
  $query = "SELECT * FROM student LIMIT 1";
  $result = mysql_query ($query);
  $fields = Mysql_num_fields ($result);
  for ($count =0; $count < $fields; $count + +)
  {
   $field = Mysql_fetch_field ($result, $count);
  Print_r ($field);
>

The results of the operation are as follows:

StdClass Object ([name] => student_id [table] => student [def] => [max_length] => 1 [not_null] =& Gt 1 [Primary_key] => 1 [multiple_key] => 0 [Unique_key] => 0 [numeric] => 1 [blob] => 0 [type] =& Gt int [unsigned] => 0 [Zerofill] => 0) stdClass Object ([name] => student_name [table] => student [D EF] => [max_length] => 5 [not_null] => 1 [primary_key] => 0 [Multiple_key] => 0 [Unique_key] =&G T 
  0 [Numeric] => 0 [blob] => 0 [Type] => string [unsigned] => 0 [Zerofill] => 0) StdClass Object ( [Name] => class_id [table] => student [def] => [max_length] => 1 [not_null] => 1 [primary_key ] => 0 [Multiple_key] => 0 [Unique_key] => 0 [numeric] => 1 [blob] => 0 [type] => int [unsig 
  Ned] => 0 [Zerofill] => 0) stdClass Object ([name] => total_score [table] => student [def] => [Max_length]=> 3 [Not_null] => 1 [primary_key] => 0 [Multiple_key] => 0 [Unique_key] => 0 [numeric] => 1

 [blob] => 0 [type] => int [unsigned] => 0 [Zerofill] => 0)

Thank you for reading, I hope to help you, thank you for your support for this site!

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.