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!