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

Source: Internet
Author: User
Tags php mysql
PHP MySQL three ways to get table field names and fields information

The information for the tables used in this example is given first:



Get table field information using DESC

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);  }? >



Operation Result:

Array (  [Field] = student_id  [Type] + int (4)  [Null] = + NO  [Key] = = PRI  [Default] = =   [Extra] = auto_increment) Array (  [Field] = Student_name  [Type] = = varchar (+)  [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);  }? >



Operation Result:

array ([Field] = student_id [Type] + int (4) [Collation] = [Null] =&G T NO [Key] = PRI [Default] = [Extra] = auto_increment [Privileges] = select,insert,update,references [ Comment] = Array ([Field] = student_name [Type] = varchar ([Collation] = latin1_swedish_ci [Null] + NO [Key] + [Default] = [Extra] = [privileges] = select,insert,update,references [Comment] =& Gt   ) Array ([Field] = class_id [Type] + int (4) [Collation] = [Null] + NO [Key] + [Default] + = [Extra] = [privileges] = select,insert,update,references [Comment] = =) Array ([Field] = Total_score [ Type] = + int (4) [Collation] = [Null] + NO [Key] = [Default] = [Extra] = [privileges] => ; select,insert,update,references [Comment] =) 



Get table field information 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] = 1  [Primary_key] = 1 [Multiple_key] = 0 [Unique_key] = 0 [numeric] + 1 [blob] + 0 [Type] = int [unsigned] = 0 [Zerofill] + 0) stdClass Object ([name] = student_name [table] + student [def] = [ Max_length] + 5 [Not_null] = 1 [Primary_key] = 0 [Multiple_key] + 0 [Unique_key] = 0 [numeric] =& Gt 0 [blob] + 0 [Type] = = string [unsigned] = 0 [Zerofill] + 0) stdClass Object ([name] = class_id [t   ABLE] = student [Def] = [Max_length] = 1 [Not_null] = 1 [Primary_key] = 0 [Multiple_key] + 0 [Unique_key] + 0 [Numeric] = 1 [blob] + 0 [type] + int [unsigned] = 0 [Zerofill] = 0) Stdcla SS Object ([name] = + total_score [table] + student [def] = [Max_length] + 3 [not_null] = 1 [prim Ary_key] = 0 [mUltiple_key] + 0 [Unique_key] = 0 [Numeric] = 1 [blob] + 0 [Type] = int [unsigned] + 0 [zero Fill] = 0)

The above is the PHP MySQL get table field name and fields information of three ways to content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!



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