0

I am trying to use PDO to connect to a SQL Server database. I have been struggling with it for a little bit now I get this Fatal error on my page where I am trying to connect to SQL Server

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712'

I have downloaded the drives from http://www.microsoft.com/en-us/download/details.aspx?id=20098 (SQLSRV30.EXE) I have placed those drivers in the ext directory where php is installed on the Server 2008 R2 server.

I also have added these 2 lines at the end of the extension list in the php.ini file

extension=php_pdo_sqlsrv_53_nts.dll
extension=php_sqlsrv_53_nts.dll

I am using PH PVersion 5.3.19 Server API CGI/FastCGI Thread Safety disabled

I do see pdo_sqlsrv but I don't see a Client API Version.

What else do I need to do to be able to use PDO to connect to the remote server that has SQL databses? Do I need to install anything on the remote servers?

this is a screenshot of my php my admin section

enter image description here

This is my connection class

<?php

class connection {

    private $connString;
    private $userName;
    private $passCode;
    private $server;
    private $pdo;
    private $errorMessage;
    protected $lastQueryTime;
    protected $lastQuery;

    private $pdo_opt = array (
                            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
                            );




    function __construct($dbName = DATABASE_NAME, $serverName = DATABASE_HOST){

        //sets credentials
        $this->setConnectionCredentials($dbName, $serverName);

        //start the connect
        $this->startConnection();

    }

    function startConnection(){


            $this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);

            if( ! $this->pdo){

                $this->errorMessage  = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
                $this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
                echo $this->getError();
            }
    }


    //this will close the PDO connection
    public function endConnection(){

        $this->pdo = null;
    }

    //return a dataset with the results
    public function getDataSet($query, $data = NULL)
    {
        $start = microtime(true);
        $cmd = $this->pdo->prepare( $query );

        $cmd->execute($data);
        $ret = $cmd->fetchAll();
        //$cmd->closeCursor();
        $this->lastQueryTime = microtime(true) - $start;
        $this->lastQuery = $query;

        return $ret;
    }



    public function processQuery($query, $data = NULL)
    {
        $start = microtime(true);
               //$this->pdo->beginTransaction();


        $cmd = $this->pdo->prepare( $query );
        $ret = $cmd->execute($data);
               //$this->pdo->commit();
               //$cmd->closeCursor();
        $this->lastQueryTime = microtime(true) - $start;
        $this->lastQuery = $query;

        return $ret;
    }


    //return last insert id
    public function lastInsertId($name = NULL) {
        if(!$this->pdo) {
            return false;
        }

        return $this->pdo->lastInsertId($name);
    }



    public function getOneResult($query, $data = NULL){
        $cmd = $this->pdo->prepare( $query );
        $cmd->execute($data);

        return $cmd->fetchColumn();
    }

    public function getError(){
        if($this->errorMessage != '')
            return $this->errorMessage;
        else
            return true;  //no errors found

    }

    //this where you need to set new server credentials with a new case statment
    function setConnectionCredentials($dbName, $serv){

        switch($serv){


            //MS SQL server
            case 'SQLSERVER':
                $this->connString   = 'sqlsrv:server='.$serv.';database='.$dbName;
                $this->userName     = 'username';  
                $this->passCode     = 'password';  
            break;

            //the defaults are predefined in the APP_configuration file - DO NOT CHANGE THE DEFAULT
            default:
                $this->connString   = 'mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME.';charset=utf8';
                $this->userName     = DATABASE_USERNAME;
                $this->passCode     = DATABASE_PASSWORD;
            break;

            }

    }


public function lastQueryTime() {
    if(!$this->lastQueryTime) {
        throw new Exception('no query has been executed yet');
    }
    return $this->lastQueryTime;
}

public function lastQuery() {
    if(!$this->lastQuery) {
        throw new Exception('no query has been executed yet');
    }
    return $this->lastQuery;
}



}



?>

This is how I use my class to pull a dataset

<?php
include('connection.php');

$sql_db = new connection('databaseName','SQLSERVER');

$call_details = $sql_db->getDataSet('SELECT
                                    LocalUserId AS loginName,
                                    RemoteNumberCallId AS PhoneNumber,
                                    SUM(CallDurationSeconds + HoldDurationSeconds + LineDurationSeconds) AS totalTalk
                                    FROM dbo.CallDetail WHERE LocalUserId = \'blah\' AND RemoteNumberCallId = \'123456789\'
                                    GROUP BY LocalUserId, RemoteNumberCallId');


$call_details->endConnection();

?>

I have even tried this code so I won't use my class and I still get the same error

 $ss = new PDO("sqlsrv:server=SQLSERVER; Database=databaseName", "userName", "Password");
Mike
  • 2,735
  • 11
  • 44
  • 68
  • Nothing. It should be good to go.. Try using `sqlsrv_query`. – Kermit Apr 25 '13 at 20:10
  • that for your comment. what is sqlsrv_query? and how do I use that? – Mike Apr 25 '13 at 20:12
  • **sqlsrv_query** is part of the **sqlsrv_** series of PHP functions which are an alternative to PDO. This will *not* help you, as your problem is outside PHP, at the driver/OS level. – Jonathan Lidbeck Jun 16 '15 at 01:16

2 Answers2

5

I have replaced the

extension=php_sqlsrv_53_nts.dll
extension=php_pdo_sqlsrv_53_nts.dll

With

extension=php_pdo_sqlsrv_53_nts_vc9.dll 
extension=php_sqlsrv_53_nts_vc9.dll

Insted on using SQLSRV30.EXE I used the SQLSRV20.EXE This worked.

@FreshPrinceOfSo Thanks for your help :)

Thanks :)

Kermit
  • 33,827
  • 13
  • 85
  • 121
Mike
  • 2,735
  • 11
  • 44
  • 68
  • 1
    After much messing around this worked for me on Win 2008 R2 x64, SQL Server Standard Edition (64-bit), IIS 6 and PHP 5.3 with php_pdo_sqlsrv_53_ts_vc9.dll & php_sqlsrv_53_ts_vc9.dll Enabled. – Colin Oct 22 '14 at 14:01
1

My personal installation for Windows Server 2008 R2 and configuration with PHP/SQL Server.

  1. Download the latest stable .zip release of VC9 x86 Non Thread Safe from PHP Downloads
  2. Extract into your PHP directory
  3. Download Microsoft Drivers 3.0 for PHP for SQL Server
  4. Extract into your PHP directory\ext
  5. Write the following extensions to php.ini

    [PHP_SQLSRV_54_NTS]

    extension=php_sqlsrv_54_nts.dll

    [PHP_PDO_SQLSRV_54_NTS]

    extension=php_pdo_sqlsrv_54_nts.dll

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • I did exactly that. But Since I already have php5.3.19 installed I went to your step 3 and I added this into my php.ini file [PHP_SQLSRV_54_NTS] extension=php_sqlsrv_54_nts.dll [PHP_PDO_SQLSRV_54_NTS] extension=php_pdo_sqlsrv_54_nts.dll. This is not working. I am not sure what is the problem. Do I need to install SQL on this server? Note that MS SQL is not installed on this server (Where I run my php script) But it is installed on the remote reserve where I am trying to connect to the SQL service – Mike Apr 25 '13 at 21:00
  • 3
    Can I VTC a comment as a duplicate? – Zane Apr 25 '13 at 21:02
  • 1
    just in case you didn't get it the first time, @FreshPrinceOfSO – swasheck Apr 25 '13 at 21:03
  • @Mike Are you sure that your PHP install is NTS? You do *not* need SQL Server installed on the box. Can we see your PHP code? – Kermit Apr 25 '13 at 21:04
  • 3
    Can I VTC a comment as a duplicate? – Zane Apr 25 '13 at 21:05
  • @FreshPrinceOfSO What PHP code you want to see and I will post it in my question. I am looking at the phpinfo page and I see: PHP Extension Build API20090626,NTS,VC9 Does this answer your question? – Mike Apr 25 '13 at 21:17
  • @FreshPrinceOfSO I just added my connection class and a usage sample. I hope that gives you more information. – Mike Apr 25 '13 at 21:29
  • @FreshPrinceOfSO When I execute this code print_r(PDO::getAvailableDrivers()); I get: Array ( [0] => mysql [1] => sqlite [2] => sqlsrv ) and when I execute this code print_r(PDO::ATTR_CLIENT_VERSION ); I get: 5 I don't know if this can give me clues to help me. – Mike Apr 25 '13 at 23:46
  • @Mike Try the sample code on the [MSDN site](http://msdn.microsoft.com/en-us/library/cc296184(v=sql.105).aspx) – Kermit Apr 26 '13 at 00:02
  • @FreshPrinceOfSO I figured it out. Check my answer. Thanks for your help :) – Mike Apr 26 '13 at 00:22