Quickstart: Use PHP to query a database in Azure SQL Database or Azure SQL Managed Instance
Applies to: Azure SQL Database Azure SQL Managed Instance
This article demonstrates how to use PHP to connect to a database in Azure SQL Database or Azure SQL Managed Instance. You can then use T-SQL statements to query data.
Prerequisites
To complete this quickstart, you need:
An Azure account with an active subscription. Create an account for free.
A database in Azure SQL Database or Azure SQL Managed Instance. You can use one of these quickstarts to create and then configure a database:
Action SQL Database SQL Managed Instance SQL Server on Azure VM Create Portal Portal Portal CLI CLI PowerShell PowerShell PowerShell Configure Server-level IP firewall rule Connectivity from a VM Connectivity from on-premises Connect to a SQL Server instance Load data Wide World Importers loaded per quickstart Restore Wide World Importers Restore Wide World Importers Restore or import Adventure Works from a BACPAC file from GitHub Restore or import Adventure Works from a BACPAC file from GitHub Important
The scripts in this article are written to use the
AdventureWorks2022
database. With a SQL Managed Instance, you must either import theAdventureWorks2022
database into an instance database or modify the scripts in this article to use the Wide World Importers database.PHP-related software installed for your operating system:
macOS, install PHP, the ODBC driver, then install the PHP Driver for SQL Server. See Step 1, 2, and 3.
Linux, install PHP, the ODBC driver, then install the PHP Driver for SQL Server. See Step 1, 2, and 3.
Get server connection information
Get the connection information you need to connect to the database in Azure SQL Database. You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.
Sign in to the Azure portal.
Navigate to the SQL Databases or SQL Managed Instances page.
On the Overview page, review the fully qualified server name next to Server name for a database in Azure SQL Database or the fully qualified server name (or IP address) next to Host for an Azure SQL Managed Instance or SQL Server in an Azure VM. To copy the server name or host name, hover over it and select the Copy icon.
Note
For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance.
Add code to query the database
In your favorite text editor, create a new file, sqltest.php.
Replace its contents with the following code. Then add the appropriate values for your server, database, user, and password.
<?php $serverName = "your_server.database.windows.net"; // update me $connectionOptions = array( "Database" => "your_database", // update me "Uid" => "your_username", // update me "PWD" => "your_password" // update me ); //Establishes the connection $conn = sqlsrv_connect($serverName, $connectionOptions); $tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid"; $getResults= sqlsrv_query($conn, $tsql); echo ("Reading data from table" . PHP_EOL); if ($getResults == FALSE) echo (sqlsrv_errors()); while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) { echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL); } sqlsrv_free_stmt($getResults); ?>
Run the code
At the command prompt, run the app.
php sqltest.php
Verify the top 20 rows are returned and close the app window.