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)!