title: PHP PDO connection with MS SQL Server using ODBC
body: <p>I have a db.php file &amp; and init.php to use within my register.php file. </p>
  
  <p>I have a database within MS SQL Server and am trying to connect to it using PDO. </p>
  
  <p>This error is showing and I can't see why. I used the same code to connect to a MySQL db and this inserted the record as should, but doesn't when writing to a MSSQLServer</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'PDOException' with message 'could not
    find driver' in C:\ooplr\classes\DB.php:14 Stack trace: #0
    C:\ooplr\classes\DB.php(14):
    PDO->__construct('mssql:host=myserver;...', 'username', 'pwd') #1
    C:\ooplr\classes\DB.php(23): DB->__construct() #2
    C:\ooplr\classes\Validate.php(8): DB::getInstance() #3
    C:\ooplr\register.php(9): Validate->__construct() #4 {main} thrown in
    C:\ooplr\classes\DB.php on line 14</p>
  </blockquote>
  
  <p><strong>The db.php page</strong></p>
  
  <pre><code>&lt;?php
      class DB {
  public static $instance = null;
  private     $_pdo = null,
              $_query = null,
              $_error = false,
              $_results = null,
              $_count = 0;
  private function __construct() {
      try {           
          $this-&gt;_pdo = new PDO('mssql:host=' . Config::get('mssql/server') . ';dbname=' . Config::get('mssql/db'), Config::get('mssql/username'), Config::get('mssql/password'));
      } catch(PDOExeption $e) {
          die($e-&gt;getMessage());
      }
  }
  public static function getInstance() {
      if(!isset(self::$instance)) {
          self::$instance = new DB();
      }
      return self::$instance;
  }
  public function query($sql, $params = array()) {
      $this-&gt;_error = false;
      if($this-&gt;_query = $this-&gt;_pdo-&gt;prepare($sql)) {
          $x = 1;
          if(count($params)) {
              foreach($params as $param) {
                  $this-&gt;_query-&gt;bindValue($x, $param);
                  $x++;
              }
          }
          if($this-&gt;_query-&gt;execute()) {
              $this-&gt;_results = $this-&gt;_query-&gt;fetchAll(PDO::FETCH_OBJ);
              $this-&gt;_count = $this-&gt;_query-&gt;rowCount();
          } else {
              $this-&gt;_error = true;
          }
      }       
      return $this;
  }
  public function get($table, $where) {
  return $this-&gt;action('SELECT *', $table, $where);
  }
  public function delete($table, $where) {
      return $this-&gt;action('DELETE', $table, $where);
  }
  public function action($action, $table, $where = array()) {
      if(count($where) === 3) {
          $operators = array('=', '&gt;', '&lt;', '&gt;=', '&lt;=');
          $field      = $where[0];
          $operator   = $where[1];
          $value      = $where[2];
          if(in_array($operator, $operators)) {
              $sql = "{$action} FROM {$table} WHERE {$field} {$operator}   ?";
              if(!$this-&gt;query($sql, array($value))-&gt;error()) {
                  return $this;
              }
          }           
          return false;
      }
  }
  public function insert($table, $fields = array()) {
      $keys   = array_keys($fields);
      $values = null;
      $x      = 1;
      foreach($fields as $value) {
          $values .= "?";
          if($x &lt; count($fields)) {
              $values .= ', ';
          }
          $x++;
      }
      $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES     ({$values})";
      if(!$this-&gt;query($sql, $fields)-&gt;error()) {
          return true;
      }
      return false;
  }
  public function update($table, $id, $fields = array()) {
      $set    = null;
      $x      = 1;
      foreach($fields as $name =&gt; $value) {
          $set .= "{$name} = ?";
          if($x &lt; count($fields)) {
              $set .= ', ';
          }
          $x++;
      }
      $sql = "UPDATE users SET {$set} WHERE id = {$id}";
      if(!$this-&gt;query($sql, $fields)-&gt;error()) {
          return true;
      }
      return false;
  }
  public function results() {
      return $this-&gt;_results;
  }
  public function first() {
      return $this-&gt;_results[0];
  }
  public function count() {
      return $this-&gt;_count;
  }
  public function error() {
      return $this-&gt;_error;
  }}?&gt;
  </code></pre>
  
  <p><strong>The init.php page</strong></p>
  
  <pre><code>  &lt;?php
      session_start();
      $GLOBALS['config'] = array(
  'mssql' =&gt; array(
      'server' =&gt; 'MyServer',
      'username'  =&gt; 'MyUsr',
      'password'  =&gt; 'Mypwd',
      'db'    =&gt; 'dbname'
  ),
  'remember' =&gt; array(
      'cookie_name'   =&gt; 'hash',
      'cookie_expiry' =&gt;  604800
  ),
  'session' =&gt; array(
      'session_name'  =&gt; 'user',
      'token_name'    =&gt; 'token'
  )
      );
      function autoload($class) {
  require_once 'classes/' . $class . '.php';
       }
      spl_autoload_register('autoload');
       require_once 'functions/sanitize.php';
      if(Cookie::exists(Config::get('remember/cookie_name'))) {
  $hash = Cookie::get(Config::get('remember/cookie_name'));
  $hashCheck = DB::getInstance()-&gt;get('users_session', array('hash', '=', $hash));
  if($hashCheck-&gt;count()) {
      $user = new User($hashCheck-&gt;first()-&gt;user_id);
      $user-&gt;login();
  }}?&gt;
  </code></pre>
  
  <p><strong>The register.php page</strong></p>
  
  <pre><code>     &lt;?php
       require 'core/init.php';
       error_reporting (E_ALL);
       ini_set('display_errors', TRUE);
       if(Input::exists()) {
   if(Token::check(Input::get('token'))) {
      $validate = new Validate();
      $validation = $validate-&gt;check($_POST, array(
          'username' =&gt; array(
              'required' =&gt; true,
              'min' =&gt; 2,
              'max' =&gt; 20,
              'unique' =&gt; 'users'),
          'password' =&gt; array(
              'required' =&gt; true,
              'min' =&gt; 6),
          'password_again' =&gt; array(
              'required' =&gt; true,
              'matches' =&gt; 'password'),
          'name' =&gt; array(
              'required' =&gt; false,
              'min' =&gt; 2,
              'max' =&gt; 50)
      ));
      if($validation-&gt;passed()) {
          $user = new User();
          $salt = Hash::salt(32);
          try {
              $user-&gt;create(array(
                  'username'  =&gt; Input::get('username'),
                  'password'  =&gt;   Hash::make(Input::get('password'), $salt),
                  'salt'      =&gt; $salt,
                  'name'      =&gt; Input::get('name'),
                  'joined'    =&gt; getdate(),
                  'group'     =&gt; 1
              ));
              Session::flash('home', 'You have been registered and can now log in!');
              Redirect::to('index.php');
          } catch(Exception $e) {
              die($e-&gt;getMessage());
          }
      } else {
          foreach($validate-&gt;errors() as $error) {
              echo $error, '&lt;br&gt;';
          }}}}?&gt;
  
       &lt;form action="" method="post"&gt;
  
  &lt;div class="field"&gt;
      &lt;label for="username"&gt;Choose a username&lt;/label&gt;
      &lt;input type="text" name="username" id="username" value="&lt;?php echo  escape(Input::get('username')); ?&gt;"&gt;
  &lt;/div&gt;
  
  &lt;div class="field"&gt;
      &lt;label for="password"&gt;Choose a password&lt;/label&gt;
      &lt;input type="password" name="password" id="password"&gt;
  &lt;/div&gt;
  
  &lt;div class="field"&gt;
      &lt;label for="password_again"&gt;Enter your password again&lt;/label&gt;
      &lt;input type="password" name="password_again" id="password_again"&gt;
  &lt;/div&gt;
  
  &lt;div class="field"&gt;
      &lt;label for="name"&gt;Your name&lt;/label&gt;
      &lt;input type="text" name="name" id="name" value="&lt;?php echo escape(Input::get('name')); ?&gt;"&gt;
  &lt;/div&gt;
  
  &lt;input type="submit" value="Register"&gt;
  &lt;input type="hidden" name="token" value="&lt;?php echo Token::generate(); ?&gt;"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>How can I get this to connect and send the data?</p>