[Company Logo Image] 

Home Up Contents Coffee Break Credits Glossary Links Search

 
How to Query a SQL Server Database from PHP

 

 

Home
Analysis Services
Azure
CLR Integration
High Availability
Open Source
Security
SQL Server 2008
SQL Server 2012
SQL Server 2014
SQL Server 2016
SQL Server 2017
SQL Server 2019
Tips
Troubleshooting
Tuning

How to Query a SQL Server Database from PHP.


Applies to: SQL Server 2012 Express on Windows Server 2012 Standard Edition, Microsoft Drivers 3.0 for PHP v5.4 for SQL Server in IIS, Microsoft Drivers 3.0 for PHP v5.4 for SQL Server in IIS, Microsoft Web Platform Installer 4.6, IIS 8.
 

This article shows how to connect to a SQL Server database and run query from a PHP program.

To be able to run our PHP program you need to perform the following steps:

1. You may not have to do this, but I created a Hyper-V virtual machine with 1.5 GB of RAM, and installed Windows Server 2012 Standard Edition.

2. Change the Windows Server computer name to a more familiar name that the default WIN-XXXXXXXXXXX. I changed the computer name MORILLOPHP.

3. Install the Web Server (IIS) role on Windows Server 2012 with all default features it requires. You can test the Web server by running the following command (Windows Logo + R): http://localhost. The IIS 8 logo should appear on Internet Explorer.

4. Download and install Microsoft Web Platform Installer 4.6 from here. Install "Microsoft Drivers 3.0 for PHP v5.4 for SQL Server in IIS" and "PHP 5.4.24" using Microsoft Web Platform Installer 4.6.

5. To test the PHP installation, save the following PHP code on the C:\inetpub\wwwroot directory and name the text file as phpinfo.php. Then run the program using Internet Explorer like this: http://localhost/phpinfo.php.

<?PHP

phpinfo();

?>

6. Install SQL Server Express 2012 with Tools using this article.

 

Creating a simple table using SQL Server Management Studio.
 

I created a simple table using SQL Server Management Studio (SSMS) and populated the table with one record. These are the steps I followed:

Open SSMS. Make a right click on Databases folder, select "New database", and provide a database name and click OK. I named the database "MorilloDB".

Now, let's create a new table by expanding the MorilloDB database folder, make a right click on the tables folder, and select "New table..." and type the column names shown below.




Make a click on the Save button and name the table "Employees".






Make a right click on the Tables folder, and select "Refresh" to make the newly created table show in Object Explorer.







Now let's create some records on the Employees table by making a right click on the table, and then select "Edit Top 200 Rows".




 

I created just one record, but you can go ahead and add some more.



 


Creating the PHP program to query the database.


Let's create the PHP program to query the table we just created above in SQL Server. Type the following program in Notepad or Notepad++ and name the program BasicProgramQuerySQLServer_1.php and store it on the C:\inetpub\wwwroot folder.










Then run the program using Internet Explorer as shown below.





 

 

 

.Send mail to webmaster@sqlcoffee.com with questions or comments about this web site.