分享方式:


如何:使用內建的 UTF-8 支援傳送及擷取 UTF-8 資料

下載 PHP 驅動程式

如果您使用 PDO_SQLSRV 驅動程式,您可以指定具有 PDO::SQLSRV_ATTR_ENCODING 屬性的編碼。 如需詳細資訊,請參閱常數 (適用於 SQL Server 之 PHP 的 Microsoft 驅動程序)

本主題的其餘部分將討論 SQLSRV 驅動程式的編碼功能。

若要將 UTF-8 編碼資料傳送或擷取至伺服器:

  1. 確定來源或目的地資料行屬於 ncharnvarchar類型。

  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 編碼資料。 此範例會更新 Production.ProductReview 資料表中指定的檢閱識別碼的「註解」資料行。 此範例也會擷取新的更新資料,並加以顯示。 請注意,「註解」資料行屬於 nvarchar(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);  
?>  

另請參閱

擷取資料

在非 Windows 中使用 ASCII 資料

更新資料 (Microsoft Drivers for PHP for SQL Server)

SQLSRV 驅動程式 API 參考

常數 (Microsoft Drivers for PHP for SQL Server)

範例應用程式 (SQLSRV 驅動程式)