[8] SQL Server – PHP

1 lutego 2022 Wyłączono przez Adam [zicherka] Nogły

[1] To jest przykład użycia SQL Server z PHP. Przykład oparty na środowisku, w którym zainstalowano PHP 7.4. Dodatkowo zainstaluj inne wymagane pakiety.

[root@vlsr01 ~]# dnf install gcc make php-pear php-pdo php-devel
[root@vlsr01 ~]# pecl install sqlsrv pdo_sqlsrv
[root@vlsr01 ~]# echo 'extension=pdo_sqlsrv.so' > /etc/php.d/99-pdo_sqlsrv.ini
[root@vlsr01 ~]# echo 'extension=sqlsrv.so' > /etc/php.d/99-sqlsrv.ini

[2] Stwórz przykładowego użytkownika i bazę danych.

[root@vlsr01 ~]# sqlcmd -S localhost -U SA
Password:
#stwórz użytkownika
1> create login cent with PASSWORD= N'password';
2> go
#stwórz [SampleDB]
1> create database SampleDB;
2> go
1> use SampleDB;
2> go
Changed database context to 'SampleDB'.

#stwórz użytkownika BD
1> create user cent for login cent;
2> go

#powiąż użytkownika [cent] jako właściciela BD
1> exec sp_addrolemember 'db_owner', 'cent';
2> go

#stwórz przykładową BD
1> create table SampleTable (
2> ID int identity(1,1) not null primary key, First_Name NVARCHAR(50), Last_Name NVARCHAR(50) );
3> insert into SampleTable (
4> First_Name, Last_Name) values (N'CentOS', N'Linux'), (N'RedHat', N'Linux'), (N'Fedora', N'Linux' );
5> go
(3 rows affected)

[3] Przykładowe użycie połączenia SQL Server i PHP.

[cent@vlsr01 ~]$ php -v
PHP 7.4.19 (cli) (built: May  4 2021 11:06:37) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

[cent@vlsr01 ~]$ mcedit use_mssql.php
#stwórz nowy plik
<?php
$serverName = "127.0.0.1";
$connectionOptions = array(
    "Database" => "SampleDB",
    "Uid" => "cent",
    "PWD" => "TajneHasło"
);

$conn = sqlsrv_connect($serverName, $connectionOptions);

// pobierz z bazy danych
$tsql= "select * from SampleTable;";
$getResults= sqlsrv_query($conn, $tsql);
echo ("Reading data from SampleTable" . PHP_EOL);
if ($getResults == FALSE)
    die(FormatErrors(sqlsrv_errors()));
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
    echo ($row['ID'] . " " . $row['First_Name'] . " " . $row['Last_Name'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);

// wstaw do SampleTable
$tsql= "insert into SampleTable (First_Name, Last_Name) values (?,?);";
$params = array('Ubuntu','Linux');
$getResults= sqlsrv_query($conn, $tsql, $params);
echo ("\nInserting a new row into SampleTable" . PHP_EOL);
$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);

// zaktualizuj SampleTable
$userToUpdate = 'Redhat';
$tsql= "update SampleTable set Last_Name = ? where First_Name = ?";
$params = array('Ootpa', $userToUpdate);
echo("\nUpdating Last_Name 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);

$tsql= "select * from SampleTable;";
$getResults= sqlsrv_query($conn, $tsql);
echo ("\nReading data from SampleTable" . PHP_EOL);
if ($getResults == FALSE)
    die(FormatErrors(sqlsrv_errors()));
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
    echo ($row['ID'] . " " . $row['First_Name'] . " " . $row['Last_Name'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);

// skasuj z SampleTable
$userToDelete = 'Ubuntu';
$tsql= "delete from SampleTable where First_Name = ?";
$params = array($userToDelete);
$getResults= sqlsrv_query($conn, $tsql, $params);
echo("\nDeleting 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);

function FormatErrors( $errors )
{
    echo "Error information: ";
    foreach ( $errors as $error )
    {
        echo "SQLSTATE: ".$error['SQLSTATE']."";
        echo "Code: ".$error['code']."";
        echo "Message: ".$error['message']."";
    }
}
?>

[cent@vlsr01 ~]$ php ./use_mssql.php
Reading data from SampleTable
1 CentOS Linux
2 RedHat Linux
3 Fedora Linux

Inserting a new row into SampleTable
1 row(s) inserted:

Updating Last_Name for user Redhat
1 row(s) updated:

Reading data from SampleTable
1 CentOS Linux
2 RedHat Ootpa
3 Fedora Linux
4 Ubuntu Linux

Deleting user Ubuntu
1 row(s) deleted: