組み込みの UTF-8 サポートを使用して UTF-8 データを送信および取得する方法

UTF-8 文字のサポートは、SQL Server Driver for PHP Version 1.1 で追加されました。

SQL Server Driver for PHP Version 1.0 では、iconv 関数を使用して手動で UTF-8 との変換を行う必要がありました。詳細については、「UTF-8 データに変換する方法および UTF-8 データを変換する方法」を参照してください。

サーバーとの間で UTF-8 エンコード データを送信または取得するには、次の操作を行います。

  1. 変換元または変換先の列の型が nchar または nvarchar であることを確認します。
  2. パラメーター配列内で PHP 型を SQLSRV_PHPTYPE_STRING('UTF-8') と指定します。または、接続オプションとして "CharacterSet" => "UTF-8" を指定します。
    接続オプションの一部として文字セットを指定する場合、ドライバーでは、他方の接続オプション文字列で同じ文字セットを使用すると見なされます。また、サーバー名とクエリの文字列も同じ文字セットを使用すると見なされます。

UTF-8 または SQLSRV_ENC_CHAR を CharacterSet に渡せることに注意してください (SQLSRV_ENC_BINARY を渡すことはできません)。既定のエンコードは SQLSRV_ENC_CHAR です。

次の例は、接続時に UTF-8 文字セットを指定することで、UTF-8 エンコード データを送信および取得する方法を示しています。この例では、指定したレビュー ID について Production.ProductReview テーブルの Comments 列を更新します。また、新しく更新したデータを取得して表示します。Comments 列の型は nvarcahr(3850) になっています。データをサーバーに送信する前に、PHP の utf8_encode 関数を使用して UTF-8 エンコードに変換します。ここでは、例として説明するためにこのようにしてあります。実際のアプリケーションでは、最初から UTF-8 エンコード データを使用します。

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

<?php

// Connect to the local server using Windows Authentication and
// specify the AdventureWorks database as the database in use. 
// 
$serverName = "MyServer";
$connectionInfo = array( "Database"=>"AdventureWorks", "CharacterSet" => "UTF-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if ( $conn === false ) {
   echo "Could not connect.<br>";
   die( print_r( sqlsrv_errors(), true));
}

// Set up the Transact-SQL query.
// 
$tsql1 = "UPDATE Production.ProductReview
          SET Comments = ?
          WHERE ProductReviewID = ?";

// Set the parameter values and put them in an array. Note that
// $comments is converted to UTF-8 encoding with the PHP function
// utf8_encode to simulate an application that uses UTF-8 encoded data. 
// 
$reviewID = 3;
$comments = utf8_encode("testing 1, 2, 3, 4.  Testing.");
$params1 = array(
                  array( $comments, null ),
                  array( $reviewID, null )
                );

// Execute the query.
// 
$stmt1 = sqlsrv_query($conn, $tsql1, $params1);

if ( $stmt1 === false ) {
   echo "Error in statement execution.<br>";
   die( print_r( sqlsrv_errors(), true));
}
else {
   echo "The update was successfully executed.<br>";
}

// Retrieve the newly updated data.
// 
$tsql2 = "SELECT Comments 
          FROM Production.ProductReview 
          WHERE ProductReviewID = ?";

// Set up the parameter array.
// 
$params2 = array($reviewID);

// Execute the query.
// 
$stmt2 = sqlsrv_query($conn, $tsql2, $params2);
if ( $stmt2 === false ) {
   echo "Error in statement execution.<br>";
   die( print_r( sqlsrv_errors(), true));
}

// Retrieve and display the data. 
// 
if ( sqlsrv_fetch($stmt2) ) {
   echo "Comments: ";
   $data = sqlsrv_get_field( $stmt2, 0 );
   echo $data."<br>";
}
else {
   echo "Error in fetching data.<br>";
   die( print_r( sqlsrv_errors(), true));
}

// Free statement and connection resources.
// 
sqlsrv_free_stmt( $stmt1 );
sqlsrv_free_stmt( $stmt2 );
sqlsrv_close( $conn);
?>

Unicode データの格納の詳細については、「Unicode データを使用した作業」を参照してください。

次の例は、最初の例と似ていますが、UTF-8 文字セットを接続上で指定するのでなく、列に UTF-8 文字セットを指定する方法を示しています。

<?php

// Connect to the local server using Windows Authentication and
// specify the AdventureWorks database as the database in use. 
// 
$serverName = "MyServer";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if ( $conn === false ) {
   echo "Could not connect.<br>";
   die( print_r( sqlsrv_errors(), true));
}

// Set up the Transact-SQL query.
// 
$tsql1 = "UPDATE Production.ProductReview
          SET Comments = ?
          WHERE ProductReviewID = ?";

// Set the parameter values and put them in an array. Note that
// $comments is converted to UTF-8 encoding with the PHP function
// utf8_encode to simulate an application that uses UTF-8 encoded data. 
// 
$reviewID = 3;
$comments = utf8_encode("testing");
$params1 = array(
                  array($comments,
                        SQLSRV_PARAM_IN,
                        SQLSRV_PHPTYPE_STRING('UTF-8')
                  ),
                  array($reviewID)
                );

// Execute the query.
// 
$stmt1 = sqlsrv_query($conn, $tsql1, $params1);

if ( $stmt1 === false ) {
   echo "Error in statement execution.<br>";
   die( print_r( sqlsrv_errors(), true));
}
else {
   echo "The update was successfully executed.<br>";
}

// Retrieve the newly updated data.
// 
$tsql2 = "SELECT Comments 
          FROM Production.ProductReview 
          WHERE ProductReviewID = ?";

// Set up the parameter array.
// 
$params2 = array($reviewID);

// Execute the query.
// 
$stmt2 = sqlsrv_query($conn, $tsql2, $params2);
if ( $stmt2 === false ) {
   echo "Error in statement execution.<br>";
   die( print_r( sqlsrv_errors(), true));
}

// Retrieve and display the data. 
// 
if ( sqlsrv_fetch($stmt2) ) {
   echo "Comments: ";
   $data = sqlsrv_get_field($stmt2, 
                            0, 
                            SQLSRV_PHPTYPE_STRING('UTF-8')
                           );
   echo $data."<br>";
}
else {
   echo "Error in fetching data.<br>";
   die( print_r( sqlsrv_errors(), true));
}

// Free statement and connection resources.
// 
sqlsrv_free_stmt( $stmt1 );
sqlsrv_free_stmt( $stmt2 );
sqlsrv_close( $conn);
?>

参照

処理手順

サンプル アプリケーション

概念

SQLSRV 定数

その他のリソース

データの取得
データの更新 (SQL Server Driver for PHP)
API リファレンス (SQL Server Driver for PHP)