|
1 |
| -# PHP Linux and Mac Drivers Installation Tutorial |
2 |
| -The following instructions assume a clean environment and show how to install PHP 7.x, the Microsoft ODBC driver, Apache, and the Microsoft drivers for PHP for Microsoft SQL Server on Ubuntu 16.04 and 17.10, RedHat 7, Debian 8 and 9, Suse 12, and macOS 10.11 and 10.12. These instructions advise installing the drivers using PECL, but you can also download the prebuilt binaries from the [Microsoft Drivers for PHP for Microsoft SQL Server](https://github.com/Microsoft/msphpsql/releases) Github project page and install them following the instructions in [Loading the Microsoft Drivers for PHP for Microsoft SQL Server](https://docs.microsoft.com/sql/connect/php/loading-the-php-sql-driver)). For an explanation of extension loading and why we do not add the extensions to php.ini, see the section on [loading the drivers](https://docs.microsoft.com/sql/connect/php/loading-the-php-sql-driver#loading-the-driver-at-php-startup). |
| 1 | +# Linux and macOS Installation Tutorial for the Microsoft Drivers for PHP for SQL Server |
| 2 | +The following instructions assume a clean environment and show how to install PHP 7.x, the Microsoft ODBC driver, Apache, and the Microsoft drivers for PHP for Microsoft SQL Server on Ubuntu 16.04, 17.10 and 18.04, RedHat 7, Debian 8 and 9, Suse 12, and macOS 10.11, 10.12 and 10.13. These instructions advise installing the drivers using PECL, but you can also download the prebuilt binaries from the [Microsoft Drivers for PHP for Microsoft SQL Server](https://github.com/Microsoft/msphpsql/releases) Github project page and install them following the instructions in [Loading the Microsoft Drivers for PHP for Microsoft SQL Server](https://docs.microsoft.com/sql/connect/php/loading-the-php-sql-driver)). For an explanation of extension loading and why we do not add the extensions to php.ini, see the section on [loading the drivers](https://docs.microsoft.com/sql/connect/php/loading-the-php-sql-driver#loading-the-driver-at-php-startup). |
3 | 3 |
|
4 |
| -These instruction install PHP 7.2 by default -- see the notes at the beginning of each section to install PHP 7.0 or 7.1. |
| 4 | +These instructions install PHP 7.2 by default -- see the notes at the beginning of each section to install PHP 7.0 or 7.1. |
5 | 5 |
|
6 | 6 | ## Contents of this page:
|
7 | 7 |
|
8 |
| -- [Installing the drivers on Ubuntu 16.04 and 17.10](#installing-the-drivers-on-ubuntu-1604-and-1710) |
| 8 | +- [Installing the drivers on Ubuntu 16.04, 17.10, and 18.04](#installing-the-drivers-on-ubuntu-1604-1710-and-1804) |
9 | 9 | - [Installing the drivers on Red Hat 7](#installing-the-drivers-on-red-hat-7)
|
10 | 10 | - [Installing the drivers on Debian 8 and 9](#installing-the-drivers-on-debian-8-and-9)
|
11 | 11 | - [Installing the drivers on Suse 12](#installing-the-drivers-on-suse-12)
|
12 | 12 | - [Installing the drivers on macOS El Capitan, Sierra and High Sierra](#installing-the-drivers-on-macos-el-capitan-sierra-and-high-sierra)
|
13 | 13 |
|
14 |
| -## Installing the drivers on Ubuntu 16.04 and 17.10 |
| 14 | +## Installing the drivers on Ubuntu 16.04, 17.10 and 18.04 |
15 | 15 |
|
16 | 16 | > [!NOTE]
|
17 | 17 | > To install PHP 7.0 or 7.1, replace 7.2 with 7.0 or 7.1 in the following commands.
|
| 18 | +> For Ubuntu 18.04, the step to add the ondrej repository is not required unless |
| 19 | +> PHP 7.0 or 7.1 is needed. However, installing PHP 7.0 or 7.1 in Ubuntu 18.04 may |
| 20 | +> not work as packages from the ondrej repository come with dependencies that may |
| 21 | +> conflict with a base Ubuntu 18.04 install. |
18 | 22 |
|
19 | 23 | ### Step 1. Install PHP
|
20 | 24 | ```
|
@@ -44,6 +48,7 @@ a2enmod mpm_prefork
|
44 | 48 | a2enmod php7.2
|
45 | 49 | echo "extension=pdo_sqlsrv.so" >> /etc/php/7.2/apache2/conf.d/30-pdo_sqlsrv.ini
|
46 | 50 | echo "extension=sqlsrv.so" >> /etc/php/7.2/apache2/conf.d/20-sqlsrv.ini
|
| 51 | +exit |
47 | 52 | ```
|
48 | 53 | ### Step 5. Restart Apache and test the sample script
|
49 | 54 | ```
|
@@ -86,11 +91,11 @@ echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini file
|
86 | 91 | echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini
|
87 | 92 | exit
|
88 | 93 | ```
|
89 |
| -An issue in PECL may prevent correct installation of the latest version of the drivers even if you have upgraded GCC. To install, download the packages and compile manually: |
| 94 | +An issue in PECL may prevent correct installation of the latest version of the drivers even if you have upgraded GCC. To install, download the packages and compile manually (similar steps for pdo_sqlsrv): |
90 | 95 | ```
|
91 | 96 | pecl download sqlsrv
|
92 |
| -tar xvzf sqlsrv-5.2.0.tgz |
93 |
| -cd sqlsrv-5.2.0/ |
| 97 | +tar xvzf sqlsrv-5.3.0.tgz |
| 98 | +cd sqlsrv-5.3.0/ |
94 | 99 | phpize
|
95 | 100 | ./configure --with-php-config=/usr/bin/php-config
|
96 | 101 | make
|
@@ -196,6 +201,7 @@ zypper install apache2 apache2-mod_php7
|
196 | 201 | a2enmod php7
|
197 | 202 | echo "extension=sqlsrv.so" >> /etc/php7/apache2/php.ini
|
198 | 203 | echo "extension=pdo_sqlsrv.so" >> /etc/php7/apache2/php.ini
|
| 204 | +exit |
199 | 205 | ```
|
200 | 206 | ### Step 5. Restart Apache and test the sample script
|
201 | 207 | ```
|
@@ -266,47 +272,47 @@ To test this sample script, create a file called testsql.php in your system's do
|
266 | 272 | <?php
|
267 | 273 | $serverName = "yourServername";
|
268 | 274 | $connectionOptions = array(
|
269 |
| - "Database" => "yourDatabase", |
270 |
| - "Uid" => "yourUsername", |
271 |
| - "PWD" => "yourPassword" |
| 275 | + "database" => "yourDatabase", |
| 276 | + "uid" => "yourUsername", |
| 277 | + "pwd" => "yourPassword" |
272 | 278 | );
|
273 | 279 |
|
274 |
| -//Establishes the connection |
| 280 | +// Establishes the connection |
275 | 281 | $conn = sqlsrv_connect($serverName, $connectionOptions);
|
276 |
| -if( $conn === false ) { |
277 |
| - die( FormatErrors( sqlsrv_errors())); |
| 282 | +if ($conn === false) { |
| 283 | + die(formatErrors(sqlsrv_errors())); |
278 | 284 | }
|
279 | 285 |
|
280 |
| -//Select Query |
281 |
| -$tsql= "SELECT @@Version as SQL_VERSION"; |
| 286 | +// Select Query |
| 287 | +$tsql = "SELECT @@Version AS SQL_VERSION"; |
282 | 288 |
|
283 |
| -//Executes the query |
284 |
| -$getResults= sqlsrv_query($conn, $tsql); |
| 289 | +// Executes the query |
| 290 | +$stmt = sqlsrv_query($conn, $tsql); |
285 | 291 |
|
286 |
| -//Error handling |
287 |
| -if ($getResults == FALSE) |
288 |
| - die(FormatErrors(sqlsrv_errors())); |
| 292 | +// Error handling |
| 293 | +if ($stmt === false) { |
| 294 | + die(formatErrors(sqlsrv_errors())); |
| 295 | +} |
289 | 296 | ?>
|
290 | 297 |
|
291 | 298 | <h1> Results : </h1>
|
292 | 299 |
|
293 | 300 | <?php
|
294 |
| -while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { |
295 |
| - echo ($row['SQL_VERSION']); |
296 |
| - echo ("<br/>"); |
| 301 | +while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { |
| 302 | + echo $row['SQL_VERSION'] . PHP_EOL; |
297 | 303 | }
|
298 | 304 |
|
299 |
| -sqlsrv_free_stmt($getResults); |
| 305 | +sqlsrv_free_stmt($stmt); |
| 306 | +sqlsrv_close($conn); |
300 | 307 |
|
301 |
| -function FormatErrors( $errors ) |
| 308 | +function formatErrors($errors) |
302 | 309 | {
|
303 |
| - /* Display errors. */ |
| 310 | + // Display errors |
304 | 311 | echo "Error information: <br/>";
|
305 |
| - foreach ( $errors as $error ) |
306 |
| - { |
307 |
| - echo "SQLSTATE: ".$error['SQLSTATE']."<br/>"; |
308 |
| - echo "Code: ".$error['code']."<br/>"; |
309 |
| - echo "Message: ".$error['message']."<br/>"; |
| 312 | + foreach ($errors as $error) { |
| 313 | + echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>"; |
| 314 | + echo "Code: ". $error['code'] . "<br/>"; |
| 315 | + echo "Message: ". $error['message'] . "<br/>"; |
310 | 316 | }
|
311 | 317 | }
|
312 | 318 | ?>
|
|
0 commit comments