How to enable Pdo_sqlsrv in PHP?

This article is for running PHPKB MSSQL Enterprise Edition (6.0 and upwards) using the Microsoft SQL Server database. It explains the steps required to install and configure Microsoft SQL Server Driver (php_sqlsrv.dll) for PHP. The steps detailed below must be performed before installing PHPKB knowledge base software. Please note, that the minimum required version of MSSQL has been stabilized to MSSQL 2005 (v.9).

Using the SQL Server Driver (SQLSRV) for PHP from Microsoft

  1. Install Microsoft SQL Server including SQL Server Management Studio. (A free version, SQL Server Express Edition is available for testing.)
    Make sure to choose mixed authentication (Windows and local accounts) to keep things simpler later. Define the "sa" account password when requested (it’s the default System Administrator account that has full access to all databases by default).
  2. Configure Windows for MSSQL.
    • By default, MSSQL listens to port 1433 for incoming TCP/IP connections and this port needs to be opened in the firewall. This is explicitly configured in the firewall installed (either Windows Firewall in the Control Panel or the configuration interface for other firewalls). If the port was changed when MSSQL was installed, then specify the correct port number to open in the firewall.
    • Confirm that TCP/IP protocol is enabled in: SQL Server Configuration Manager -> Network Configuration -> Protocols -> TCP/IP enabled
  3. Create and configure a new database.
    Open "SQL Server Management Studio" and create a new empty database.
  4. Install PHP and a web server.
    • PHP can be downloaded from www.php.net
    • If IIS is used as the web server, IIS 7.0 or later is recommended with FastCGI and WinCache.
  5. Install the SQL Server Driver for PHP.
    On the web server, install SQL Server Driver for PHP including all the pre-requisites listed on the download page. Always use the latest version available for your environment).
    Note: It is critical to install the SQL Server Native Access Client version documented on the download page of the SQL Server Driver for PHP PDO.
  6. Configure PHP to use the appropriate PDO SQLSRV driver. In php.ini, set the following:
    • For PHP 5.3.2 (or later)

      [PHP_SQLSRV]
      extension=php_pdo_sqlsrv_53_nts_vc9.dll

    The Microsoft documentation for the SQL Server Driver for PHP is available at this link.
  7. Set the following settings in your php.ini file

    mssql.textlimit = 2147483647
    mssql.textsize = 2147483647

  8. Restart or start your web server and test the database connection with the sample PHP script code below.
  9. Just create a new file called db-test.php in any text editor such as Notepad or TextPad. Copy the code from the example below and change (’server_name’, ’db_name’, ’db_user’, ’db_password’) variables to match with your database settings. Then, execute this test script from localhost (http://localhost/db-test.php).

    Source Code (db-test.php)

    <?php

         // Database connection details

         $dsn = ’sqlsrv:server=SERVER2012\MSSQL;database=phpkb’;

         $user = ’dbuser’;

         $pass = ’secure_password’;

     

         // Initialise

         $conn = null;
         try {
             // Database connection
             $pdoObj = new PDO($dsn, $user, $pass);

             if(is_object($pdoObj)){

               echo ’Connection established successfully.’;

             }
         }
         catch(PDOException $pe){
             // Throw exception
             echo ’Critical Error: Unable to connect to Database Server because: ’.$pe->getMessage();
         }

    ?>

  10. If everything works alright then you can proceed with the Installation of PHPKB Knowledge Base Software.

DEBUG INFORMATION

If PHP still cannot communicate with the database server, turn display_startup_errors to "On" in the php.ini file, then restart the web server and check for any errors that may indicate incorrect DLL versions or missing dependencies. These error reports which are turned off by default in PHP can be very useful in locating a problem with the installation of new extensions.

Do you want support for the script installation or customization? Submit your request for customization of our scripts, support for the existing web application, and new development service.

You can connect with the support team directly via email at [email protected] for any inquiry/help.

How to enable SQLSRV in PHP?

The SQLSRV extension is enabled by adding appropriate DLL file to your PHP extension directory and the corresponding entry to the php. ini file. The SQLSRV download comes with 8 driver files, four of which are for PDO support. The most recent version of the driver is available for download here: » SQLSRV download.

How to add SQLSRV in PHP?

Using the SQL Server Driver (SQLSRV) for PHP from Microsoft.
Install Microsoft SQL Server including SQL Server Management Studio. ... .
Configure Windows for MSSQL. ... .
Create and configure a new database. ... .
Install PHP and a web server. ... .
Install the SQL Server Driver for PHP. ... .
Configure PHP to use the appropriate PDO SQLSRV driver..

How to install PDO PHP extension?

Pdo ( Portable Data Object ) needs to be installed if it is not done before. For windows platform go to control panel > Add remove program ( or Programs and features ) > Select your PHP installation and click Change. If you are installing PHP fresh then in the setup wizard you can select PDO from Extensions link.

How to use Sqlsrv_connect in PHP?

For example: $connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"AdventureWorks"); */ $serverName = "(local)"; $connectionInfo = array( "Database"=>"AdventureWorks"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.