php mysql获取表字段名称和字段信息的三种方法

5年以前  |  阅读数:507 次  |  编程语言:PHP 

php mysql获取表字段名称和字段信息的三种方法

先给出本实例中使用的表的信息:

使用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
    )

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例