Bagikan melalui


Cara: Mengirim dan Mengambil Data UTF-8 Menggunakan Dukungan Built-In UTF-8

Unduh driver PHP

Jika Anda menggunakan driver PDO_SQLSRV, Anda dapat menentukan pengodean dengan atribut PDO::SQLSRV_ATTR_ENCODING. Untuk informasi selengkapnya, lihat Konstanta (Driver Microsoft untuk PHP untuk SQL Server).

Sisa topik ini membahas pengodean dengan driver SQLSRV.

Untuk mengirim atau mengambil data yang dikodekan UTF-8 ke server:

  1. Pastikan bahwa kolom sumber atau tujuan berjenis nchar atau nvarchar.

  2. Tentukan jenis PHP seperti SQLSRV_PHPTYPE_STRING('UTF-8') dalam array parameter. Atau, tentukan "CharacterSet" => "UTF-8" sebagai opsi koneksi.

    Saat Anda menentukan kumpulan karakter sebagai bagian dari opsi koneksi, driver mengasumsikan bahwa string opsi koneksi lainnya menggunakan set karakter yang sama. Nama server dan string kueri juga diasumsikan menggunakan kumpulan karakter yang sama.

Anda dapat meneruskan UTF-8 atau SQLSRV_ENC_CHAR ke CharacterSet, tetapi Anda tidak dapat meneruskan SQLSRV_ENC_BINARY. Pengodean default adalah SQLSRV_ENC_CHAR.

Contoh koneksi

Contoh berikut menunjukkan cara mengirim dan mengambil data yang dikodekan UTF-8 dengan menentukan kumpulan karakter UTF-8 saat membuat koneksi. Contoh memperbarui kolom Komentar dari tabel Production.ProductReview untuk ID tinjauan tertentu. Contoh ini juga mengambil data yang baru diperbarui dan menampilkannya. Perhatikan bahwa kolom Komentar berjenis nvarchar(3850). Perhatikan juga bahwa sebelum data dikirim ke server, data dikonversi ke pengodean UTF-8 menggunakan fungsi utf8_encode PHP. Ini dilakukan hanya untuk tujuan demonstrasi. Dalam skenario aplikasi nyata, Anda akan mulai dengan data yang dikodekan UTF-8.

Contohnya mengasumsikan bahwa SQL Server dan database AdventureWorks diinstal di komputer lokal. Semua output ditulis ke browser ketika contoh dijalankan dari browser.

<?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);  
?>  

Untuk informasi tentang menyimpan data Unicode, lihat Bekerja dengan Data Unicode.

Contoh kolom

Contoh berikut mirip dengan sampel pertama tetapi alih-alih menentukan karakter UTF-8 yang diatur pada koneksi, sampel ini menunjukkan cara menentukan kumpulan karakter UTF-8 pada kolom.

<?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);  
?>  

Lihat juga

Mengambil Data

Bekerja dengan Data ASCII di non-Windows

Memperbarui Data (Driver Microsoft untuk PHP untuk SQL Server)

Referensi API Driver SQLSRV

Konstanta (Driver Microsoft untuk PHP untuk SQL Server)

Contoh Aplikasi (Driver SQLSRV)