共用方式為


Getting Started with PHP 7 + SQL Server and Azure SQL Database on Linux (Ubuntu) with Apache

This blog will show you how to create a sample PHP 7 application and connect to SQL Server or Azure SQL DB from your Linux (Ubuntu) machine.

Step 1: Install  pre-requisites

Ubuntu 15.04

[snippet slug=odbc-ubuntu-15-04 lang=bsh]

Ubuntu 15.10

[snippet slug=odbc-ubuntu-15-10 lang=bsh]

Ubuntu 16.04

[snippet slug=odbc-ubuntu-16-04 lang=bsh]

*Note: On Ubuntu, you need to make sure you install PHP 7 before you proceed to step 2. The Microsoft PHP Drivers for SQL Server will only work for PHP 7+. You can install PHP following the instructions here.

Step 2: Install Apache

[snippet slug=apache lang=bsh]

Step 3: Install the Microsoft PHP Drivers for SQL Server

[snippet slug=pecl lang=bsh]

Step 4: Add the Microsoft PHP Drivers for SQL Server to php.ini

[snippet slug=add-extension lang=bsh]

Restart Apache to load the new php.ini file

[snippet slug=restart-apache lang=bsh]

Step 5: Create your sample app

  • Navigate to /var/www/html and create a new file called testsql.php. Copy and paste the following code in tetsql.php and change the servername, username, password and databasename.

[snippet slug=code-sample lang=bsh]

Step 6: Run your sample app

  • Go to your browser and type in https://localhost/testsql.php
  • You should be able to connect to your SQL Server/Azure SQL Database and see the following results

results

If you have any questions/issues report it on our GitHub repository.

Meet Bhagdev (meetb@microsoft.com)

MSFTlovesPHP

Comments

  • Anonymous
    November 01, 2016
    Hi Meet,This is a great tutorial so far. I'm stuck, however, and unable to connect to my SQL Server.I am running Ubuntu 16.04 Server as a virtual machine (VMWare Workstation 12 on Windows 10), network connection set to Bridged. I have SQL Express running on my Windows 10 machine, and I can ping the local IP address (192.168.0.13) and get a response. It's setup to use the default port 1433 and the default instance name, \SQLEXPRESS, and my firewall is disabled during this. I was successfully able to connect and query this SQL Server db using a commercial driver, so I feel that my SQL Server instance is setup ok . However, adding "print_r(sqlsrv_errors(),true)" just after the sqlsrv_connect() in the test script displays the following error message:------------------------------------------------Could not connect. Array ( [0] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired [message] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired ) [1] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 258 [code] => 258 [2] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: Error code 0x102 [message] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: Error code 0x102 ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 258 [code] => 258 [2] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [unixODBC][Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) ) ------------------------------------------------Any suggestions would be helpful, as I am stuck on this at the moment. I think I've checked on the basics but am probably missing something. Thank you, in advance!------Jamie
    • Anonymous
      November 06, 2016
      The comment has been removed
  • Anonymous
    December 05, 2016
    Hi,when running "sudo pecl install sqlsrv-4.0.7" on a Ubuntu 16 server, I get this message:/tmp/pear/temp/sqlsrv/shared/xplat.h:36:17: fatal error: sql.h: No such file or directory #include ^compilation terminated.make: *** [conn.lo] Error 1ERROR: `make' failedSame with the next command.Can you help me?
    • Anonymous
      January 23, 2017
      You need to install unixodbc-dev. Did you install the ODBC driver and the unixODBC Driver Manager?
  • Anonymous
    February 24, 2017
    Hi, what a great tutorial! When I install this, I got an error. Can you tell me what is wrong?-- pecl install sqlsrvNo releases available for package "pecl.php.net/sqlsrv"install failed----
    • Anonymous
      June 01, 2017
      Are you still running into this issue? What version of Linux and PHP are you using?