Share via


방법: Linux 및 macOS에서 ASCII 데이터 보내기 및 검색

PHP 드라이버 다운로드

이 문서에서는 ASCII(UTF-8이 아닌) 로캘이 Linux 또는 macOS 시스템에 생성되거나 설치된 것으로 가정합니다.

서버에 ASCII 문자 집합을 보내거나 검색하려면 다음을 수행합니다.

  1. 원하는 로캘이 시스템 환경의 기본값이 아닌 경우, 처음 연결하기 전에 setlocale(LC_ALL, $locale)을 호출해야 합니다. PHP setlocale() 함수는 현재 스크립트에 대해서만 로캘을 변경하고 첫 번째 연결을 만든 후 호출되는 경우에는 무시될 수 있습니다.

  2. SQLSRV 드라이버를 사용할 때 연결 옵션으로 'CharacterSet' => SQLSRV_ENC_CHAR를 지정할 수 있지만, 이 단계는 기본 인코딩이므로 선택 사항입니다.

  3. PDO_SQLSRV 드라이버를 사용하는 경우 두 가지 방법이 있습니다. 우선, 연결할 때 PDO::SQLSRV_ATTR_ENCODINGPDO::SQLSRV_ENCODING_SYSTEM으로 설정합니다(연결 옵션 설정 예는 PDO::__construct 참조). 또는 연결에 성공하면 이 $conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM); 줄을 추가합니다.

연결 리소스(SQLSRV) 또는 연결 개체(PDO_SQLSRV)의 인코딩을 지정할 때 드라이버는 다른 연결 옵션 문자열이 동일한 인코딩을 사용하는 것으로 가정합니다. 서버 이름 및 쿼리 문자열도 동일한 문자 집합을 사용한다고 가정합니다.

PDO_SQLSRV 드라이버의 기본 인코딩은 SQLSRV 드라이버와 달리 UTF-8(PDO::SQLSRV_ENCODING_UTF8)입니다. 이러한 상수에 대한 자세한 내용은 상수(Microsoft Drivers for PHP for SQL Server)를 참조하세요.

예시

다음 예시에서는 연결 설정 전에 특정 로캘을 지정하여 SQL Server용 PHP 드라이버를 사용하여 ASCII 데이터를 보내고 검색하는 방법을 보여줍니다. 여러 Linux 플랫폼의 로캘은 macOS의 동일한 로캘과 이름이 다를 수 있습니다. 예를 들어 Linux에서는 US ISO-8859-1(라틴 1) 로캘의 이름이 en_US.ISO-8859-1이지만, macOS에서는 en_US.ISO8859-1입니다.

이 예시에서는 SQL Server가 서버에 설치되어 있다고 가정합니다. 모든 출력은 브라우저에서 예시를 실행할 때 브라우저에 기록됩니다.

<?php  
  
// SQLSRV Example
//
// Setting locale for the script is only necessary if Latin 1 is not the default 
// in the environment
$locale = strtoupper(PHP_OS) === 'LINUX' ? 'en_US.ISO-8859-1' : 'en_US.ISO8859-1';
setlocale(LC_ALL, $locale);
        
$serverName = 'MyServer';
$database = 'Test';
$connectionInfo = array('Database'=>'Test', 'UID'=>$uid, 'PWD'=>$pwd);
$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 to create a test table
//   
$stmt = sqlsrv_query($conn, "CREATE TABLE [Table1] ([c1_int] int, [c2_varchar] varchar(512))");

// Insert data using a parameter array 
//
$tsql = "INSERT INTO [Table1] (c1_int, c2_varchar) VALUES (?, ?)";
  
// Execute the query, $value being some ASCII string
//   
$stmt = sqlsrv_query($conn, $tsql, array(1, array($value, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))));
  
if ($stmt === false) {
    echo "Error in statement execution.<br>";  
    die(print_r(sqlsrv_errors(), true));  
}  
else {  
    echo "The insertion was successfully executed.<br>";  
}  
  
// Retrieve the newly inserted data
//   
$stmt = sqlsrv_query($conn, "SELECT * FROM Table1");
$outValue = null;  
if ($stmt === false) {  
    echo "Error in statement execution.<br>";  
    die(print_r(sqlsrv_errors(), true));  
}  
  
// Retrieve and display the data
//   
if (sqlsrv_fetch($stmt)) {  
    $outValue = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
    echo "Value: " . $outValue . "<br>";
    if ($value !== $outValue) {
        echo "Data retrieved, \'$outValue\', is unexpected!<br>";
    }
}  
else {  
    echo "Error in fetching data.<br>";  
    die(print_r(sqlsrv_errors(), true));  
}  

// Free statement and connection resources
//   
sqlsrv_free_stmt($stmt);  
sqlsrv_close($conn);  
?>  
<?php  
  
// PDO_SQLSRV Example:
//
// Setting locale for the script is only necessary if Latin 1 is not the default 
// in the environment
$locale = strtoupper(PHP_OS) === 'LINUX' ? 'en_US.ISO-8859-1' : 'en_US.ISO8859-1';
setlocale(LC_ALL, $locale);
        
$serverName = 'MyServer';
$database = 'Test';

try {
    $conn = new PDO("sqlsrv:Server=$serverName;Database=$database;", $uid, $pwd);
    $conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM);
    
    // Set up the Transact-SQL query to create a test table
    //   
    $stmt = $conn->query("CREATE TABLE [Table1] ([c1_int] int, [c2_varchar] varchar(512))");
    
    // Insert data using parameters, $value being some ASCII string
    //
    $stmt = $conn->prepare("INSERT INTO [Table1] (c1_int, c2_varchar) VALUES (:var1, :var2)");
    $stmt->bindValue(1, 1);
    $stmt->bindParam(2, $value);
    $stmt->execute();
    
    // Retrieve and display the data
    //
    $stmt = $conn->query("SELECT * FROM [Table1]");
    $outValue = null;
    if ($row = $stmt->fetch()) {
        $outValue = $row[1];
        echo "Value: " . $outValue . "<br>";
        if ($value !== $outValue) {
            echo "Data retrieved, \'$outValue\', is unexpected!<br>";
        }
    }
} catch (PDOException $e) {
    echo $e->getMessage() . "<br>";
} finally {
    // Free statement and connection resources
    //
    unset($stmt);
    unset($conn);
}

?>  

참고 항목

데이터 검색
UTF-8 데이터 작업데이터 업데이트(Microsoft Drivers for PHP for SQL Server)
SQLSRV 드라이버 API 참조
상수(Microsoft Drivers for PHP for SQL Server)
예시 애플리케이션(SQLSRV 드라이버)