Latest web development tutorials

PHP mysqli_fetch_field () function

PHP MySQLi Reference Manual PHP MySQLi Reference Manual

Returns the result set to the next field (column) and the output of each field name, and the maximum length of the form:

<?php 
// 假定数据库用户名:root,密码:123456,数据库:w3big 
$con=mysqli_connect("localhost","root","123456","w3big"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

$sql="SELECT name,url FROM websites ORDER BY alexa";


if ($result=mysqli_query($con,$sql))
{
	// 获取所有列的字段信息
    while ($fieldinfo = mysqli_fetch_field($result)) {

        printf("字段名:     %s\n", $fieldinfo->name);
        echo "<br>";
        printf("数据表:    %s\n", $fieldinfo->table);
        echo "<br>";
        printf("最大长度: %d\n", $fieldinfo->max_length);
        echo "<br>";
    }
	// 释放结果集
	mysqli_free_result($result);
}

mysqli_close($con);
?>

Definition and Usage

mysqli_fetch_field () function Fetch next field (column) from the results, and return as an object.


grammar

mysqli_fetch_field( result ) ;

参数 描述
result 必需。规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。

technical details

return value: Returns an object that contains field definition information. If no information is available it returns FALSE. This object has the following properties:
  • name - the name of the column
  • orgname - original column name (if you specify an alias)
  • table - a table name
  • orgtable - the original name of the table (if you specify an alias)
  • def - retained as the default value, the current is always ""
  • db - database (in PHP 5.3.6 the new)
  • catalog - directory name is always "def" (since PHP 5.3.6 onwards)
  • The maximum width of the field - max_length
  • length - specified in the table definition field width
  • Character Set number field - charsetnr
  • Flag field - flags
  • type - data type for the field
  • decimals - integer field digits after the decimal point
PHP version: 5+


PHP MySQLi Reference Manual PHP MySQLi Reference Manual