https://www.php.net/manual/en/book.sqlsrv.php
PHP: SQLSRV - Manual
There are no user contributed notes for this page.
www.php.net
Microsoft Drivers for PHP for SQL Server - PHP drivers for SQL Server
The Microsoft Drivers for PHP for SQL Server enable integration with SQL Server for PHP applications via PHP extensions.
learn.microsoft.com
드라이버 다운로드
Download the Microsoft Drivers for PHP for SQL Server - PHP drivers for SQL Server
Download the Microsoft Drivers for PHP for SQL Server to develop PHP applications that connect to SQL Server and Azure SQL Database.
learn.microsoft.com
실제다운로드 페이지
설치일기준 버전
Version information
- Release number: 5.11.0
- Released: February 28, 2023
ODBC 드라이버
SQL Server용 ODBC 드라이버 다운로드 - ODBC Driver for SQL Server
Microsoft ODBC Driver for SQL Server를 다운로드하여 SQL Server 및 Azure SQL Database에 연결하는 네이티브 코드 애플리케이션을 개발합니다.
learn.microsoft.com
SQLCMD
Download the Microsoft Drivers for PHP for SQL Server - PHP drivers for SQL Server
Download the Microsoft Drivers for PHP for SQL Server to develop PHP applications that connect to SQL Server and Azure SQL Database.
learn.microsoft.com
해당부분 설치후 압축푼후 내 버전에 맞게 처리한다.
PHP 8.1버전 사용중에 TS인 상황이므로
php_pdo_sqlsrv_81_ts_x64.dll
php_sqlsrv_81_ts_x64.dll
파일을 복사하여
PHP설치폴더의 하위폴더 ext 파일에 복사하여 넣는다.
php.ini 파일에 extension부분에 해당 파일 둘을 추가한다.
extension=php_sqlsrv_81_ts_x64.dll
extension=php_pdo_sqlsrv_81_ts_x64.dll
<?php
$serverName = "localhost";
$connectionOptions = array(
"Database" => "SampleDB",
"Uid" => "sa",
"PWD" => "your_password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
//Insert Query
echo ("Inserting a new row into table" . PHP_EOL);
$tsql= "INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);";
$params = array('Jake','United States');
$getResults= sqlsrv_query($conn, $tsql, $params);
$rowsAffected = sqlsrv_rows_affected($getResults);
if ($getResults == FALSE or $rowsAffected == FALSE)
die(FormatErrors(sqlsrv_errors()));
echo ($rowsAffected. " row(s) inserted: " . PHP_EOL);
sqlsrv_free_stmt($getResults);
//Update Query
$userToUpdate = 'Nikita';
$tsql= "UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?";
$params = array('Sweden', $userToUpdate);
echo("Updating Location for user " . $userToUpdate . PHP_EOL);
$getResults= sqlsrv_query($conn, $tsql, $params);
$rowsAffected = sqlsrv_rows_affected($getResults);
if ($getResults == FALSE or $rowsAffected == FALSE)
die(FormatErrors(sqlsrv_errors()));
echo ($rowsAffected. " row(s) updated: " . PHP_EOL);
sqlsrv_free_stmt($getResults);
//Delete Query
$userToDelete = 'Jared';
$tsql= "DELETE FROM TestSchema.Employees WHERE Name = ?";
$params = array($userToDelete);
$getResults= sqlsrv_query($conn, $tsql, $params);
echo("Deleting user " . $userToDelete . PHP_EOL);
$rowsAffected = sqlsrv_rows_affected($getResults);
if ($getResults == FALSE or $rowsAffected == FALSE)
die(FormatErrors(sqlsrv_errors()));
echo ($rowsAffected. " row(s) deleted: " . PHP_EOL);
sqlsrv_free_stmt($getResults);
//Read Query
$tsql= "SELECT Id, Name, Location FROM TestSchema.Employees;";
$getResults= sqlsrv_query($conn, $tsql);
echo ("Reading data from table" . PHP_EOL);
if ($getResults == FALSE)
die(FormatErrors(sqlsrv_errors()));
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['Id'] . " " . $row['Name'] . " " . $row['Location'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
function FormatErrors( $errors )
{
/* Display errors. */
echo "Error information: ";
foreach ( $errors as $error )
{
echo "SQLSTATE: ".$error['SQLSTATE']."";
echo "Code: ".$error['code']."";
echo "Message: ".$error['message']."";
}
}
?>
추가로 한글깨짐을 방지하기위해 접속구문에 CharacterSet을 추가한다.
$connectionOptions = array(
"Database" => "SampleDB",
"Uid" => "sa",
"PWD" => "your_password",
"CharacterSet" => "UTF-8"
);
'데이터베이스 > MSSQL' 카테고리의 다른 글
컴퓨터에 api-ms-win-crt-runtime-l1-1-0.dll이(가) 없어 프로그램을 시작할 수 없습니다. (0) | 2020.03.06 |
---|---|
데이터베이스에 대한 배타적 잠금을 얻을 수 없습니다. (0) | 2019.07.22 |
OPEN SYMMETRIC KEY 를통한 암호화 복호 (0) | 2019.06.12 |
geography 자료형 (0) | 2019.02.28 |
저번달 1일 마지막일 이번달 1일 마지막일 (0) | 2018.12.20 |