php mysqlでテーブルのフィールド名とフィールド情報を取得する3つの方法
まず、この例で使用されているテーブル情報を与えます:
テーブルのフィールド情報を取得するにはdescを使用します
phpコードは次のとおりです:
<?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); } ?>
実行結果:
Array ( [Field] => student_id [Type] => int(4) [Null] => NO [Key] => PRI [Default] => [Extra] => auto_increment ) Array ( [Field] => student_name [Type] => varchar(50) [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] => )
SHOW FULL FIELDSを使用してテーブルフィールド情報を取得します
phpコードは次のとおりです:
<?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); } ?>
実行結果:
Array ( [Field] => student_id [Type] => int(4) [Collation] => [Null] => NO [Key] => PRI [Default] => [Extra] => auto_increment [Privileges] => select,insert,update,references [Comment] => ) Array ( [Field] => student_name [Type] => varchar(50) [Collation] => latin1_swedish_ci [Null] => NO [Key] => [Default] => [Extra] => [Privileges] => select,insert,update,references [Comment] => ) 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] => )
mysql_fetch_field メソッドを使用してテーブルのフィールド情報を取得します
php コードは次のとおりです:
<?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); } ?>
実行結果は次のとおりです:
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] => 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 [unsigned] => 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 )
上記は 3 つのメソッドの内容です。テーブルのフィールド名とフィールド情報を取得するための php mysql、その他の関連コンテンツ PHP の中国語 Web サイト (www.php.cn) にご注意ください。