sqlsrv_num_fields

PHP ドライバーのダウンロード

アクティブな結果セット内のフィールド数を取得します。 任意の準備されたステートメントに対して、実行前または実行後に、この関数を呼び出すことができます。

構文

  
sqlsrv_num_fields( resource $stmt)  

パラメーター

$stmt: 対象の結果セットがアクティブなステートメント。

戻り値

アクティブな結果セットのフィールド数を表す整数値。 エラーが発生すると、ブール値 false が返されます。

次の例では、AdventureWorks データベースの HumanResources.Department テーブル内にある先頭の 3 つの行について、すべてのフィールドを取得するクエリを実行します。 sqlsrv_num_fields 関数で、結果セットのフィールド数を判断します。 この結果を利用して、返される各行のフィールドを反復処理してデータを表示します。

この例では、ローカル コンピューターに SQL Server および AdventureWorks データベースがインストールされていることを前提にしています。 コマンド ラインからこの例を実行すると、すべての出力はコンソールに書き込まれます。

<?php  
/* Connect to the local server using Windows Authentication and   
specify the AdventureWorks database as the database in use. */  
$serverName = "(local)";  
$connectionInfo = array( "Database"=>"AdventureWorks");  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn === false )  
{  
     echo "Could not connect.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Define and execute the query. */  
$tsql = "SELECT TOP (3) * FROM HumanResources.Department";  
$stmt = sqlsrv_query($conn, $tsql);  
if( $stmt === false)  
{  
     echo "Error in executing query.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Retrieve the number of fields. */  
$numFields = sqlsrv_num_fields( $stmt );  
  
/* Iterate through each row of the result set. */  
while( sqlsrv_fetch( $stmt ))  
{  
     /* Iterate through the fields of each row. */  
     for($i = 0; $i < $numFields; $i++)  
     {  
          echo sqlsrv_get_field($stmt, $i,   
                   SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))." ";  
     }  
     echo "\n";  
}  
  
/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt );  
sqlsrv_close( $conn );  
?>  

参照

SQLSRV ドライバー API リファレンス

sqlsrv_field_metadata

ドキュメントのコード例について