Microsoft Drivers for PHP for SQL Server

repository·dev·Indexed 23 days ago

https://github.com/microsoft/msphpsql

PHP extensions providing procedural (SQLSRV) and PDO (PDO_SQLSRV) interfaces for interacting with SQL Server databases. These drivers act as a bridge between PHP and the Microsoft ODBC Driver, supporting SQL Server 2012 and later, including Azure SQL DB. Documentation covers installation on Windows, Ubuntu (Apache and Nginx), Red Hat (RHEL), and Debian, as well as instructions for building drivers from source using builddrivers.py.

Tokens
6.4K
Snippets
11
Records
19
Agent score
84%

What's inside Microsoft Drivers for PHP for SQL Server

  1. Overview of Microsoft Drivers for PHP for SQL Server

    dev

    The Microsoft Drivers for PHP for Microsoft SQL Server are PHP extensions used to read and write SQL Server data from PHP scripts. The package provides two distinct interfaces:

    1. SQLSRV extension: Provides a procedural interface.
    2. PDO_SQLSRV extension: Implements PHP Data Objects (PDO) for accessing data.

    These drivers support all editions of SQL Server 2012 and later (including Azure SQL DB). They rely on the Microsoft ODBC Driver for SQL Server to handle low-level communication with the database.

  2. Install Microsoft PHP Drivers on Red Hat (RHEL)

    dev

    To install the drivers on Red Hat 8 or 9, use the Remi repository to install PHP and then use PECL for the drivers.

    1. Install PHP: Enable EPEL and Remi repositories, reset the PHP module, and install the desired version (e.g., php:remi-8.4). Ensure php-pdo, php-pear, and php-devel are installed.
    2. Install Prerequisites: Install the Microsoft ODBC driver for SQL Server for Linux.
    3. Install Drivers: Use pecl install for sqlsrv and pdo_sqlsrv. Manually append the extension loading to the PHP .ini scan directory. Alternatively, use yum install php-sqlsrv if available in the Remi repo.
    4. Configure Apache & SELinux: Install httpd. If SELinux is in Enforcing mode, you must allow Apache to connect to databases using setsebool -P httpd_can_network_connect_db 1.
    5. Restart: Restart Apache using apachectl restart.
    # Step 1: Install PHP (Example for RHEL 8/9)
    sudo su
    dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
    dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
    dnf install yum-utils
    dnf module reset php
    dnf module install php:remi-8.4
    dnf update
    dnf install php-pdo php-pear php-devel
    
    # Step 3: Install Drivers
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    sudo su
    echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/30-pdo_sqlsrv.ini
    echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20-sqlsrv.ini
    exit
    
    # Step 4: Apache & SELinux
    sudo yum install httpd
    sudo setsebool -P httpd_can_network_connect_db 1
    
    # Step 5: Restart
    sudo apachectl restart
  3. Manually build drivers from source on Windows

    dev

    Follow these steps to manually integrate and compile the drivers into your PHP source tree:

    1. Prepare Source: Download the source directory from this repository.
    2. Prepare Shared Folder: Copy the shared folder as a subfolder inside both the sqlsrv and pdo_sqlsrv folders.
    3. Integrate with PHP: Copy the sqlsrv and/or pdo_sqlsrv folders into your PHP source ext subdirectory.
    4. Rebuild Configuration: Run buildconf --force to update the configure.js script.
    5. Generate Makefile: Run configure.bat with driver-specific options:
      • For SQLSRV: --enable-sqlsrv=shared
      • For PDO_SQLSRV: --enable-pdo --with-pdo-sqlsrv=shared
      • For Non-Thread Safe (NTS): Add --disable-zts.
    6. Compile: Run nmake. (You may run nmake clean first if needed).
    7. Install:
      • Use nmake install OR
      • Manually copy the compiled php_sqlsrv.dll and/or php_pdo_sqlsrv.dll from your php.exe directory to the PHP ext subfolder.
  4. Build drivers using builddrivers.py (Interactive Mode)

    dev

    The builddrivers.py script provides an interactive way to build extensions for PHP on Windows.

    1. Open a regular cmd prompt.
    2. Navigate to the directory containing builddrivers.py and buildtools.py.
    3. Run the script: py builddrivers.py.
    4. Follow the interactive prompts (use lowercase for answers):
      • PHP Version: Enter values like 8.2.0. For pre-releases, use the tag name without the php- prefix (e.g., 8.0.0beta3).
      • 64-bit?: (yes/no)
      • Thread safe?: (yes/no)
      • Driver?: (sqlsrv or pdo_sqlsrv)
      • Debug enabled?: (yes/no)
      • Download source from GitHub?: (yes/no)

    Note: The script creates a php-sdk folder in C:\ by default. For ongoing development, it is recommended to keep this directory.

    py builddrivers.py
  5. Prerequisites for using Microsoft PHP Drivers

    dev

    Before installing the drivers, ensure your environment meets the following requirements:

    Client Machine:

    • PHP Versions: 8.3.x, 8.4.x, or 8.5.x.
    • ODBC Driver: Microsoft ODBC Driver 17 or 18 must be installed.
    • Web Server: If using IIS or Apache, it must be configured to run PHP.

    Server Side:

    • Windows: Microsoft SQL Server 2012 and above.
    • Linux: Microsoft SQL Server 2016 and above.
  6. Install Microsoft PHP Drivers on Ubuntu (PHP-FPM & Nginx)

    dev

    If you are using Nginx instead of Apache, you must use PHP-FPM.

    1. Install PHP-FPM: Install php8.4-fpm along with other dependencies.
    2. Install Prerequisites: Install the Microsoft ODBC driver for SQL Server for Linux.
    3. Install Drivers via PECL: Install sqlsrv and pdo_sqlsrv via PECL. Ensure you set the PECL config to use the FPM php.ini path. Create the .ini files in mods-available and enable them.
    4. Configure Nginx: Edit /etc/nginx/sites-available/default to include index.php in the index list and configure the location ~ \.php$ block to pass scripts to the PHP-FPM socket (e.g., unix:/run/php/php8.4-fpm.sock).
    5. Restart: Restart both PHP-FPM and Nginx.
    # Step 1: Install PHP-FPM
    sudo su
    add-apt-repository ppa:ondrej/php -y
    apt-get update
    apt-get install php8.4 php8.4-dev php8.4-fpm php8.4-xml -y --allow-unauthenticated
    
    # Step 3: Install Drivers
    sudo pecl config-set php_ini /etc/php/8.4/fpm/php.ini
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    sudo su
    printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.4/mods-available/sqlsrv.ini
    printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.4/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -v 8.4 sqlsrv pdo_sqlsrv
    
    # Step 4: Configure Nginx (Manual edit required in /etc/nginx/sites-available/default)
    # index index.html index.htm index.nginx-debian.html index.php;
    # location ~ \.php$ {
    #         include snippets/fastcgi-php.conf;
    #         fastcgi_pass unix:/run/php/php8.4-fpm.sock;
    # }
    
    # Step 5: Restart
    sudo systemctl restart php8.4-fpm
    sudo systemctl restart nginx.service
  7. Install the drivers on Windows

    dev

    Drivers are available as pre-compiled extensions on the releases page. They are provided in:

    • Thread-safe (TS) and Non-thread-safe (NTS) versions.
    • 32-bit and 64-bit versions.

    To load the drivers:

    1. Place the driver files in your PHP extension directory.
    2. Enable the extensions in your php.ini file by adding:
      extension=php_sqlsrv.dll
      extension=php_pdo_sqlsrv.dll
      (Note: Substitute the exact filenames provided in your downloaded package).
    3. If necessary, set the extension directory:
      extension_dir = "C:\PHP\ext"
    4. Restart your Web server (e.g., IIS or Apache) to apply changes.
    extension=php_sqlsrv.dll
    extension=php_pdo_sqlsrv.dll
    extension_dir = "C:\PHP\ext"
  8. Install the PHP drivers on Alpine

    dev

    To install the Microsoft PHP drivers on Alpine Linux:

    1. Install PHP: Enable the edge/community repository in /etc/apk/repositories. Install php84, php84-dev, php84-pear, php84-pdo, php84-openssl, autoconf, make, and g++. (Note: symbolic links for php, phpize, pecl, and php-config are recommended).
    2. Install Prerequisites: Install the Microsoft ODBC driver for SQL Server for Alpine.
    3. Install PHP Drivers: Use pecl to install sqlsrv and pdo_sqlsrv. Append the extensions to the PHP .ini scan directory using 10_pdo_sqlsrv.ini and 20_sqlsrv.ini.
    4. Configure Apache: Install php84-apache2 and apache2.
    5. Restart Apache: Run sudo rc-service apache2 restart.
    # Step 1: Install PHP
    # Add http://<mirror>/alpine/edge/community to /etc/apk/repositories
    sudo su
    apk update
    apk add php84 php84-dev php84-pear php84-pdo php84-openssl autoconf make g++
    
    # Step 3: Install drivers
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    sudo su
    echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/10_pdo_sqlsrv.ini
    echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/20_sqlsrv.ini
    
    # Step 4: Apache
    sudo apk add php84-apache2 apache2
    
    # Step 5: Restart
    sudo rc-service apache2 restart
  9. Install Microsoft PHP Drivers on Debian

    dev

    Follow these steps to install the drivers on Debian using the Sury PHP repository.

    1. Install PHP: Add the Sury PHP repository and install PHP 8.4, dev tools, XML, and intl extensions.
    2. Install Prerequisites: Install the Microsoft ODBC driver for SQL Server for Linux.
    3. Locale Setup: It is recommended to generate a locale (e.g., en_US.UTF-8) to ensure correct PHP output in browsers.
    4. Install Drivers: Use pecl install for sqlsrv and pdo_sqlsrv, then create the .ini files in mods-available and enable them with phpenmod.
    5. Configure Apache: Install libapache2-mod-php8.4 and configure the MPM modules (mpm_prefork).
    6. Restart: Restart Apache.
    # Step 1: Install PHP
    sudo su
    apt-get install curl apt-transport-https
    wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
    apt-get update
    apt-get install -y php8.4 php8.4-dev php8.4-xml php8.4-intl
    
    # Step 2: Locale (Optional but recommended)
    sudo su
    sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen
    locale-gen
    
    # Step 3: Install Drivers
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    sudo su
    printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.4/mods-available/sqlsrv.ini
    printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.4/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -v 8.4 sqlsrv pdo_sqlsrv
    
    # Step 4: Apache
    sudo su
    apt-get install libapache2-mod-php8.4 apache2
    a2dismod mpm_event
    a2enmod mpm_prefork
    a2enmod php8.4
    
    # Step 5: Restart
    sudo service apache2 restart
  10. Install the PHP drivers on Suse

    dev

    To install the Microsoft PHP drivers on Suse, follow these steps:

    1. Install PHP: Use zypper to add the appropriate repository (replacing <SuseVersion> with your version, e.g., SLE_15_SP3) and install php8, php8-pdo, php8-devel, and php8-openssl.
    2. Install Prerequisites: Install the Microsoft ODBC driver for SQL Server for Suse.
    3. Install PHP Drivers: Use pecl to install sqlsrv and pdo_sqlsrv. After installation, you must manually append the extension configuration to your PHP .ini scan directory.
    4. Configure Apache: Install apache2 and apache2-mod_php8, enable the PHP module, and append the extensions to /etc/php8/apache2/php.ini.
    5. Restart Apache: Run sudo systemctl restart apache2 to apply changes.
    # Step 1: Install PHP
    sudo su
    zypper -n ar -f https://download.opensuse.org/repositories/devel:languages:php/<SuseVersion>/devel:languages:php.repo
    zypper --gpg-auto-import-keys refresh
    zypper -n install php8 php8-pdo php8-devel php8-openssl
    
    # Step 3: Install drivers
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    sudo su
    echo extension=pdo_sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/pdo_sqlsrv.ini
    echo extension=sqlsrv.so >> `php --ini | grep "Scan for additional .ini files" | sed -e "s|.*:\s*||"`/sqlsrv.ini
    exit
    
    # Step 4: Apache
    sudo su
    zypper install apache2 apache2-mod_php8
    a2enmod php8
    echo "extension=sqlsrv.so" >> /etc/php8/apache2/php.ini
    echo "extension=pdo_sqlsrv.so" >> /etc/php8/apache2/php.ini
    exit
    
    # Step 5: Restart
    sudo systemctl restart apache2
  11. Install Microsoft PHP Drivers on Ubuntu (Apache)

    dev

    To install the sqlsrv and pdo_sqlsrv drivers on Ubuntu using Apache, follow these steps. These instructions use PHP 8.4 by default; replace 8.4 with 8.3 or 8.5 if using those versions.

    1. Install PHP: Add the ppa:ondrej/php repository and install PHP 8.4, dev tools, and XML support.
    2. Install Prerequisites: You must manually install the Microsoft ODBC driver for SQL Server for Linux following the official Microsoft documentation.
    3. Install Drivers via PECL: Use pecl install for both sqlsrv and pdo_sqlsrv, then create the necessary .ini files in mods-available and enable them using phpenmod.
    4. Configure Apache: Install libapache2-mod-php8.4 and switch the MPM module from mpm_event to mpm_prefork to support PHP.
    5. Restart: Restart the Apache service to apply changes.
    # Step 1: Install PHP
    sudo su
    add-apt-repository ppa:ondrej/php -y
    apt-get update
    apt-get install php8.4 php8.4-dev php8.4-xml -y --allow-unauthenticated
    
    # Step 3: Install Drivers
    sudo pecl install sqlsrv
    sudo pecl install pdo_sqlsrv
    sudo su
    printf "; priority=20\nextension=sqlsrv.so\n" > /etc/php/8.4/mods-available/sqlsrv.ini
    printf "; priority=30\nextension=pdo_sqlsrv.so\n" > /etc/php/8.4/mods-available/pdo_sqlsrv.ini
    exit
    sudo phpenmod -v 8.4 sqlsrv pdo_sqlsrv
    
    # Step 4: Configure Apache
    sudo su
    apt-get install libapache2-mod-php8.4 apache2
    a2dismod mpm_event
    a2enmod mpm_prefork
    a2enmod php8.4
    exit
    
    # Step 5: Restart
    sudo service apache2 restart