PHP mysqli_fetch_field() Function

In this chapter you will learn:

  1. Definition for PHP mysqli_fetch_field() Function
  2. Syntax for PHP mysqli_fetch_field() Function
  3. Parameter for PHP mysqli_fetch_field() Function
  4. Return for PHP mysqli_fetch_field() Function
  5. Example - returns the next column in the result set, then print each field's name, table, and max length

Definition

The mysqli_fetch_field() function returns the next column in the result set as an object.

Syntax

PHP mysqli_fetch_field() Function has the following syntax.

mysqli_fetch_field(result);

Parameter

ParameterIs RequiredDescription
resultRequired.Result set returned by mysqli_query(), mysqli_store_result() or mysqli_use_result()

Return

It returns an object containing column definition or FALSE for failure. The returning object has the following properties:

Property NameMeaning
namename of the column
orgnameoriginal column name (if an alias is used)
tablename of table
orgtableoriginal table name (if an alias is used)
defreserved for default values
dbdatabase
catalogcatalog name, always "def"
max_lengthmaximum width of field
lengthwidth of field as specified in table definition
charsetnrcharacter set number for the field
flagsbit-flags for the field
typedata type used for the field
decimalsfor integer fields; the number of decimals used

Example

The following code returns the next column in the result set, then print each field's name, table, and max length.


<?php/*  ja v  a 2  s.c  o  m*/
$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT name FROM emp";

if ($result=mysqli_query($con,$sql)){
  // Get field information for all fields
  while ($fieldinfo=mysqli_fetch_field($result)){
     print $fieldinfo->name;
     print $fieldinfo->table;
     print $fieldinfo->max_length;
  }
  mysqli_free_result($result);
}

mysqli_close($con);
?>

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP mysqli_fetch_field_direct() Function
  2. Syntax for PHP mysqli_fetch_field_direct() Function
  3. Parameter for PHP mysqli_fetch_field_direct() Function
  4. Return for PHP mysqli_fetch_field_direct() Function
  5. Example - returns meta-data for a single column in the result set
Home » PHP Tutorial » PHP MySQLi Functions
PHP mysqli_affected_rows() Function
PHP mysqli_autocommit() Function
PHP mysqli_change_user() Function
PHP mysqli_character_set_name() Function
PHP mysqli_close() Function
PHP mysqli_commit() Function
PHP mysqli_connect() Function
PHP mysqli_connect_errno() Function
PHP mysqli_connect_error() Function
PHP mysqli_data_seek() Function
PHP mysqli_dump_debug_info() Function
PHP mysqli_errno() Function
PHP mysqli_error() Function
PHP mysqli_error_list() Function
PHP mysqli_fetch_all() Function
PHP mysqli_fetch_array() Function
PHP mysqli_fetch_assoc() Function
PHP mysqli_fetch_field() Function
PHP mysqli_fetch_field_direct() Function
PHP mysqli_fetch_fields() Function
PHP mysqli_fetch_lengths() Function
PHP mysqli_fetch_object() Function
PHP mysqli_fetch_row() Function
PHP mysqli_field_count() Function
PHP mysqli_field_seek() Function
PHP mysqli_field_tell() Function
PHP mysqli_free_result() Function
PHP mysqli_get_charset() Function
PHP mysqli_get_client_info() Function
PHP mysqli_get_client_stats() Function
PHP mysqli_get_client_version() Function
PHP mysqli_get_connection_stats() Function
PHP mysqli_get_host_info() Function
PHP mysqli_get_proto_info() Function
PHP mysqli_get_server_info() Function
PHP mysqli_get_server_version() Function
PHP mysqli_info() Function
PHP mysqli_init() Function
PHP mysqli_insert_id() Function
PHP mysqli_kill() Function
PHP mysqli_more_results() Function
PHP mysqli_multi_query() Function
PHP mysqli_next_result() Function
PHP mysqli_num_fields() Function
PHP mysqli_num_rows() Function
PHP mysqli_options() Function
PHP mysqli_ping() Function
PHP mysqli_query() Function
PHP mysqli_real_connect() Function
PHP mysqli_real_escape_string() Function
PHP mysqli_refresh() Function
PHP mysqli_rollback() Function
PHP mysqli_select_db() Function
PHP mysqli_set_charset() Function
PHP mysqli_sqlstate() Function
PHP mysqli_ssl_set() Function
PHP mysqli_stat() Function
PHP mysqli_stmt_init() Function
PHP MySQL mysqli_thread_id() Function
PHP mysqli_thread_safe() Function