SQL Server Driver for PHP 1.1 버전에는 UTF-8 문자에 대한 지원이 추가되었습니다.
SQL Server Driver for PHP 1.0 버전에서는 iconv 함수를 사용하여 UTF-8 데이터를 수동으로 변환해야 했습니다. 자세한 내용은 방법: UTF-8 데이터 변환을 참조하십시오.
UTF-8 인코딩 데이터를 서버로 전송하거나 검색하려면
- 원본 또는 대상 열이 nchar 또는 nvarchar 형식인지 확인합니다.
- 매개 변수 배열에 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);
?>
유니코드 데이터 저장에 대한 자세한 내용은 유니코드 데이터 작업을 참조하십시오.
다음 예는 첫 번째 예제와 비슷하지만 연결에 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);
?>
참고 항목
태스크
개념
관련 자료
데이터 검색
데이터 업데이트(SQL Server Driver for PHP)
API 참조(SQL Server Driver for PHP)