이 항목에서는 SQL Server Driver for PHP를 사용하여 UTF-8 인코딩 데이터를 수동으로 전송하고 검색하는 방법에 대해 설명합니다. 이 항목의 절차에서는 PHP iconv 함수를 사용합니다. 자세한 내용은 iconv 설명서를 참조하십시오.
SQL Server Driver for PHP 1.0 버전에서는 이 방법을 사용해서만 UTF-8 데이터를 전송하거나 검색할 수 있습니다. 하지만 1.1 버전에서는 UTF-8 데이터를 변환할 필요가 없습니다. 자세한 내용은 방법: 기본 제공 UTF-8 지원을 사용하여 UTF-8 데이터 전송 및 검색을 참조하십시오.
UTF-8 인코딩 데이터를 서버로 전송
이 절차에서는 서버로 UTF-8 인코딩 데이터를 전송하는 방법에 대해 설명합니다.
UTF-8 인코딩 데이터를 서버로 전송하려면
대상 열이 nchar 또는 nvarchar 형식인지 확인합니다.
PHP iconv 함수를 사용하여 데이터를 UTF-16LE 인코딩으로 변환합니다. 예를 들어, $data가 UTF-8 인코딩 데이터를 포함하는 변수인 경우 다음 코드를 사용하면 UTF-16LE 인코딩으로 변환됩니다.
$data = iconv("utf-8", "utf-16le", $data);매개 변수 배열에 PHP 형식을 SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)으로 지정합니다. 예를 들어, $data가 전송될 매개 변수인 경우 다음 코드에서는 매개 변수 배열의 PHP 형식을 정의합니다.
$params = array( array($data, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY) ) );
UTF-8 인코딩 데이터 검색
이 절차에서는 UTF-8 인코딩 데이터를 검색하는 방법에 대해 설명하며 검색하려는 데이터가 UTF-16LE 인코딩을 사용하는 서버에 저장되어 있다고 가정합니다(위 절차 참조).
UTF-8 인코딩 데이터를 검색하려면
sqlsrv_get_field 함수를 사용하여 데이터를 검색합니다. sqlsrv_get_field 함수를 사용하여 데이터를 검색하는 방법은 방법: 단일 필드 검색을 참조하십시오.
PHP 형식을 SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)으로 지정합니다. 예를 들어, 다음 코드에서는 PHP 형식을 지정합니다.
$data = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY));PHP 형식을 지정하는 방법은 방법: PHP 데이터 형식 지정을 참조하십시오.
PHP iconv 함수를 사용하여 데이터를 UTF-8 인코딩으로 변환합니다. 예를 들어, 다음 코드에서는 $data를 UTF-16LE 인코딩에서 UTF-8 인코딩으로 변환합니다.
$data = iconv("utf-16le", "utf-8", $data);
예
다음 예에서는 UTF-8 인코딩 데이터를 전송 및 검색하는 방법을 보여 줍니다. 먼저 지정된 검토 ID에 대해 Production.ProductReview 테이블의 Comments 열을 업데이트한 다음 새로 업데이트된 데이터를 검색하여 표시합니다. Comments 열은 nvarcahr(3850) 형식이며 PHP utf8_encode 함수를 사용하여 UTF-8 인코딩으로 변환된 다음 데이터가 서버로 전송됩니다. utf8_encode.은 예시용으로만 사용됩니다. 응용 프로그램 시나리오에서는 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 = "(local)";
$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 for demonstration purposes only. In an application
scenario you would begin with UTF-8 encoded data. Before sending the
UTF-8 data to the server, it is converted to UTF-16LE with the PHP iconv
function. */
$reviewID = 4;
$comments = utf8_encode("[Insert comment here.]");
$comments = iconv("utf-8", "utf-16le", $comments);
$params1 = array(
array($comments,
null,
SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)),
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. Note the PHPTYPE specification and the
use of the PHP iconv function to convert the data from UTF-16LE encoding
to UTF-8 encoding. */
if( sqlsrv_fetch($stmt2) )
{
echo "Comments: ";
$data = sqlsrv_get_field($stmt2,
0,
SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY));
$data = iconv("utf-16le", "utf-8", $data);
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)