PHP mysqli_fetch_field() 関数

❮ PHP MySQLi リファレンス

結果セットの次のフィールド(列)を返し、各フィールドの名前、テーブル、および最大長を出力します:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Get field information for all fields
  while ($fieldinfo=mysqli_fetch_field($result))
    {
    printf("Name: %s\n",$fieldinfo->name);
    printf("Table: %s\n",$fieldinfo->table);
    printf("max. Len: %d\n",$fieldinfo->max_length);
    }
  // Free result set
  mysqli_free_result($result);
}

mysqli_close($con);
?>

定義と用法

mysqli_fetch_field() 関数は、結果セットの次のフィールド(列)をオブジェクトとして返します。


構文

mysqli_fetch_field(result);

パラメータ 説明
result 必須。mysqli_query()、mysqli_store_result()またはmysqli_use_result()が返す結果セットの識別子を指定する

技術内容
返り値: フィールド定義情報を含むオブジェクトを返します。情報がない場合はFALSEを返します。オブジェクトには、次のプロパティがあります:
  • name - 列名
  • orgname - 元の列名(エイリアスが指定されている場合)
  • table - テーブル名
  • orgtable - 元のテーブル名(エイリアスが指定されている場合)
  • def - デフォルト値のために予約されています、現在は常に ""
  • db - データベース(PHP 5.3.6の新機能)
  • catalog - カタログ名、常に "def"(PHP 5.3.6以降)
  • max_length - フィールドの最大幅
  • length - テーブル定義で指定されたフィールドの幅
  • charsetnr - フィールドの文字セット番号
  • flags - フィールドのビットフラグ
  • type - フィールドのデータ型
  • decimals - フィールドの桁数(integer 型のフィールド)
PHP バージョン: 5+

❮ PHP MySQLi リファレンス