|
| 1 | +# Create a PHP app that connects to SQL Server and executes queries using Visual Studio Code |
| 2 | + |
| 3 | +> These examples may be used with Azure SQL Database |
| 4 | +
|
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. Ensure PHP is installed on your system. You can download PHP [here](https://www.php.net/downloads.php). In this example, PHP version 8.2 is used. |
| 8 | + |
| 9 | + |
| 10 | +## Step 2.1 Install the PHP Drivers for SQL Server |
| 11 | + |
| 12 | +1. Install the Microsoft PHP Drivers for SQL Server. You can download the drivers from the [download page](https://aka.ms/downloadmsphpsql). In this example, Microsoft Drivers 5.11 for PHP for SQL Server are used. |
| 13 | + |
| 14 | +1. Next, copy the php_sqlsrv_82_ts_x64.dll and php_pdo_sqlsrv_82_ts_x64.dll files into your PHP extensions directory. This directory is usually the ext directory in your main PHP install folder. |
| 15 | + |
| 16 | +1. Enable Microsoft PHP Drivers for SQL Server by modifying the **php.ini** file. First, navigate to where you have PHP installed. If you do not find the **php.ini** file, make a copy of either **php.ini-development** or **php.ini-production** (depending on whether your system is a development environment or production environment) and rename it **php.ini**. |
| 17 | + |
| 18 | +1. Using the windows terminal, change the directory to where you found the php.ini file. Now run the following command: |
| 19 | + |
| 20 | +```terminal |
| 21 | + echo extension=php_sqlsrv_82_ts_x64.dll >> php.ini |
| 22 | + echo extension=php_pdo_sqlsrv_82_ts_x64.dll >> php.ini |
| 23 | +``` |
| 24 | + |
| 25 | +## Step 2.2 Create a database for your application |
| 26 | + |
| 27 | +1. Create the database using sqlcmd. |
| 28 | + |
| 29 | +```terminal |
| 30 | +sqlcmd -S localhost -U sa -P your_password -Q "CREATE DATABASE SampleDB;" |
| 31 | +``` |
| 32 | + |
| 33 | +## Step 2.3 Create a PHP app that connects to SQL Server and executes queries |
| 34 | + |
| 35 | +1. Start by making a directory for the sample. |
| 36 | + |
| 37 | +```terminal |
| 38 | +mkdir SqlServerSample |
| 39 | +cd SqlServerSample |
| 40 | +``` |
| 41 | + |
| 42 | +1. Using your favorite text editor, create a new file called connect.php in the SqlServerSample folder. Paste the code below inside into the new file. |
| 43 | + |
| 44 | +```php |
| 45 | +<?php |
| 46 | + $serverName = "localhost"; |
| 47 | + $connectionOptions = array( |
| 48 | + "Database" => "SampleDB", |
| 49 | + "Uid" => "sa", |
| 50 | + "PWD" => "your_password" |
| 51 | + ); |
| 52 | + //Establishes the connection |
| 53 | + $conn = sqlsrv_connect($serverName, $connectionOptions); |
| 54 | + if($conn) |
| 55 | + echo "Connected!" |
| 56 | +?> |
| 57 | +``` |
| 58 | + |
| 59 | +1. Run your PHP script from the terminal. |
| 60 | + |
| 61 | +```terminal |
| 62 | +php connect.php |
| 63 | +``` |
| 64 | + |
| 65 | +```results |
| 66 | +Connected! |
| 67 | +``` |
| 68 | + |
| 69 | +1. Execute the T-SQL scripts below in the terminal with sqlcmd to create a schema, table, and insert a few rows. |
| 70 | + |
| 71 | +```terminal |
| 72 | +sqlcmd -S localhost -U sa -P your_password -d SampleDB -Q "CREATE SCHEMA TestSchema;" |
| 73 | +sqlcmd -S localhost -U sa -P your_password -d SampleDB -Q "CREATE TABLE TestSchema.Employees (Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, Name NVARCHAR(50), Location NVARCHAR(50));" |
| 74 | +sqlcmd -S localhost -U sa -P your_password -d SampleDB -Q "INSERT INTO TestSchema.Employees (Name, Location) VALUES (N'Jared', N'Australia'), (N'Nikita', N'India'), (N'Tom', N'Germany');" |
| 75 | +sqlcmd -S localhost -U sa -P your_password -d SampleDB -Q "SELECT * FROM TestSchema.Employees;" |
| 76 | +``` |
| 77 | + |
| 78 | +1. Using your favorite text editor, create a new file called crud.php in the SqlServerSample folder. Paste the code below inside into the new file. This will insert, update, delete, and read a few rows. |
| 79 | + |
| 80 | +```php |
| 81 | +<?php |
| 82 | +$serverName = "localhost"; |
| 83 | +$connectionOptions = array( |
| 84 | + "Database" => "SampleDB", |
| 85 | + "Uid" => "sa", |
| 86 | + "PWD" => "your_password" |
| 87 | +); |
| 88 | + |
| 89 | +//Establishes the connection |
| 90 | +$conn = sqlsrv_connect($serverName, $connectionOptions); |
| 91 | + |
| 92 | +//Insert Query |
| 93 | +echo ("Inserting a new row into table" . PHP_EOL); |
| 94 | +$tsql= "INSERT INTO TestSchema.Employees (Name, Location) VALUES (?,?);"; |
| 95 | +$params = array('Jake','United States'); |
| 96 | +$getResults= sqlsrv_query($conn, $tsql, $params); |
| 97 | +$rowsAffected = sqlsrv_rows_affected($getResults); |
| 98 | +if ($getResults == FALSE or $rowsAffected == FALSE) |
| 99 | + die(FormatErrors(sqlsrv_errors())); |
| 100 | +echo ($rowsAffected. " row(s) inserted: " . PHP_EOL); |
| 101 | + |
| 102 | +sqlsrv_free_stmt($getResults); |
| 103 | + |
| 104 | +//Update Query |
| 105 | + |
| 106 | +$userToUpdate = 'Nikita'; |
| 107 | +$tsql= "UPDATE TestSchema.Employees SET Location = ? WHERE Name = ?"; |
| 108 | +$params = array('Sweden', $userToUpdate); |
| 109 | +echo("Updating Location for user " . $userToUpdate . PHP_EOL); |
| 110 | + |
| 111 | +$getResults= sqlsrv_query($conn, $tsql, $params); |
| 112 | +$rowsAffected = sqlsrv_rows_affected($getResults); |
| 113 | +if ($getResults == FALSE or $rowsAffected == FALSE) |
| 114 | + die(FormatErrors(sqlsrv_errors())); |
| 115 | +echo ($rowsAffected. " row(s) updated: " . PHP_EOL); |
| 116 | +sqlsrv_free_stmt($getResults); |
| 117 | + |
| 118 | +//Delete Query |
| 119 | +$userToDelete = 'Jared'; |
| 120 | +$tsql= "DELETE FROM TestSchema.Employees WHERE Name = ?"; |
| 121 | +$params = array($userToDelete); |
| 122 | +$getResults= sqlsrv_query($conn, $tsql, $params); |
| 123 | +echo("Deleting user " . $userToDelete . PHP_EOL); |
| 124 | +$rowsAffected = sqlsrv_rows_affected($getResults); |
| 125 | +if ($getResults == FALSE or $rowsAffected == FALSE) |
| 126 | + die(FormatErrors(sqlsrv_errors())); |
| 127 | +echo ($rowsAffected. " row(s) deleted: " . PHP_EOL); |
| 128 | +sqlsrv_free_stmt($getResults); |
| 129 | + |
| 130 | + |
| 131 | +//Read Query |
| 132 | +$tsql= "SELECT Id, Name, Location FROM TestSchema.Employees;"; |
| 133 | +$getResults= sqlsrv_query($conn, $tsql); |
| 134 | +echo ("Reading data from table" . PHP_EOL); |
| 135 | +if ($getResults == FALSE) |
| 136 | + die(FormatErrors(sqlsrv_errors())); |
| 137 | +while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { |
| 138 | + echo ($row['Id'] . " " . $row['Name'] . " " . $row['Location'] . PHP_EOL); |
| 139 | +} |
| 140 | +sqlsrv_free_stmt($getResults); |
| 141 | + |
| 142 | +function FormatErrors( $errors ) |
| 143 | +{ |
| 144 | + /* Display errors. */ |
| 145 | + echo "Error information: "; |
| 146 | + |
| 147 | + foreach ( $errors as $error ) |
| 148 | + { |
| 149 | + echo "SQLSTATE: ".$error['SQLSTATE'].""; |
| 150 | + echo "Code: ".$error['code'].""; |
| 151 | + echo "Message: ".$error['message'].""; |
| 152 | + } |
| 153 | +} |
| 154 | +?> |
| 155 | +``` |
| 156 | + |
| 157 | +1. Run your PHP script from the terminal. |
| 158 | + |
| 159 | +```terminal |
| 160 | +php crud.php |
| 161 | +``` |
| 162 | + |
| 163 | +```results |
| 164 | +Inserting a new row into table |
| 165 | +1 row(s) inserted: |
| 166 | +Updating Location for user Nikita |
| 167 | +1 row(s) updated: |
| 168 | +Deleting user Jared |
| 169 | +1 row(s) deleted: |
| 170 | +Reading data from table |
| 171 | +2 Nikita Sweden |
| 172 | +3 Tom Germany |
| 173 | +4 Jake United States |
| 174 | +``` |
| 175 | + |
| 176 | +> Congratulations! You have created your first PHP app with SQL Server! Check out the next section to learn about how you can make your PHP faster with SQL Server's Columnstore feature. |
0 commit comments