方法:PHP データ型を指定する

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

PDO_SQLSRV ドライバーを使用する場合、PDOStatement::bindColumn と PDOStatement::bindParam を使用してサーバーからデータを取得するときに PHP データ型を指定できます。 詳細については、「 PDOStatement::bindColumn 」および「 PDOStatement::bindParam 」を参照してください。

次の手順では、SQLSRV ドライバーを使用してサーバーからデータを取得するときに PHP データ型を指定する方法をまとめています。

  1. sqlsrv_query または sqlsrv_prepare/sqlsrv_execute の組み合わせで Transact-SQL クエリを設定して実行します。

  2. データの行を sqlsrv_fetchを使用した読み取りに使用できるようにします。

  3. 省略可能な 3 つ目のパラメーターとして指定した目的の PHP データ型の sqlsrv_get_field を使用して、返される行のフィールド データを取得します。 省略可能な 3 つ目のパラメーターが指定されていない場合、既定の PHP 型に従ってデータが返されます。 既定の PHP の戻り値の型については、「 Default PHP Data Types」を参照してください。

    PHP データ型の指定に使用される定数については、「定数 (Microsoft Drivers for PHP for SQL Server)」の PHPTYPE のセクションを参照してください。

例

次の例では、AdventureWorks データベースの Production.ProductReview テーブルから行を取得します。 返される各行で、 ReviewDate フィールドは文字列として取得され、 Comments フィールドはストリームとして取得されます。 ストリーム データを表示するには、PHP の fpassthru 関数を使用します。

この例では、ローカル コンピューターに 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));  
}  
  
/* Set up the Transact-SQL query. */  
$tsql = "SELECT ReviewerName,   
                ReviewDate,  
                Rating,   
                Comments   
         FROM Production.ProductReview   
         WHERE ProductID = ?   
         ORDER BY ReviewDate DESC";  
  
/* Set the parameter value. */  
$productID = 709;  
$params = array( $productID);  
  
/* Execute the query. */  
$stmt = sqlsrv_query($conn, $tsql, $params);  
if( $stmt === false )  
{  
     echo "Error in statement execution.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
  
/* Retrieve and display the data. The first and third fields are  
retrieved according to their default types, strings. The second field  
is retrieved as a string with 8-bit character encoding. The fourth  
field is retrieved as a stream with 8-bit character encoding.*/  
while ( sqlsrv_fetch( $stmt))  
{  
   echo "Name: ".sqlsrv_get_field( $stmt, 0 )."\n";  
   echo "Date: ".sqlsrv_get_field( $stmt, 1,   
                       SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR))."\n";  
   echo "Rating: ".sqlsrv_get_field( $stmt, 2 )."\n";  
   echo "Comments: ";  
   $comments = sqlsrv_get_field( $stmt, 3,   
                            SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));  
   fpassthru( $comments);  
   echo "\n";   
}  
  
/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?>  

この例では、2 つ目のフィールド (ReviewDate) を文字列として取得すると、SQL Server の DATETIME データ型のミリ秒の精度が保持されます。 既定では、SQL Server の DATETIME データ型は PHP DateTime オブジェクトとして取得され、ミリ秒の精度は失われます。

4 つ目のフィールド (Comments) をストリームとして取得するのは、例として示すためです。 既定では、SQL Server データ型 nvarchar(3850) は文字列として取得されるので、ほとんどの場合に使用できます。

注記

クエリを実行する前に、 sqlsrv_field_metadata 関数を使用して、型情報などのフィールド情報を取得することもできます。