Laravel 5 and Sql Server

11

I need a help, I made several attempts to connect Laravel with SQL Server and all without success. However, I was able to run pure php using sqlsrv_connect and Code Igniter.

Windows 10 64 bits.
Wampserver 2.5 32 bits.
PHP version: 5.5.12 (TS, no php info: Thread Safety enabled).
PHP Extension: Build    API20121212, TS, VC11.
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll

I'm trying to connect to a db that already exists in network out of my development computer, but in any case, it will print from my sql server manager:

HomeIhavetriedtoconnectonlywiththeservernameinsteadof"ip, port". I tried to make a pure connect with new PDO and it did not work, the message that returns is from "could not find driver". And in Laravel the error is the same: "PDOException in Connector.php line 55: could not find driver".

I gave some debugging and saw that the code stops at the file's "createConnection":

  

Vendor \ laravel \ framework \ src \ Illuminate \ Database \ Connectors \ Connector.php

And the code stops exactly where you try to make the new PDO:

try{
  $pdo = new PDO($dsn, $username, $password, $options);
}catch(Exception $e){
  $pdo = $this->tryAgainIfCausedByLostConnection(
    $e, $dsn, $username, $password, $options
  );
}

return $pdo;

I imagine that although I am able to use with sqlsrv_connect (Code Igniter does so instead of PDO ??) there are some configuration errors, or in the file's .dll preventing me from using the PDO.

.env configuration file :

APP_ENV=local
APP_DEBUG=true
APP_KEY=[a key da aplicação tá okay]

DB_HOST=[IP,PORT]
DB_DATABASE=[nome do banco de dados]
DB_USERNAME=[usuário]
DB_PASSWORD=[senha]

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

database.php file :

'default' => env('DB_CONNECTION', 'sqlsrv'),
'connections' não mexi, está ~original~:
'sqlsrv' => [
  'driver'   => 'sqlsrv',
  'host'     => env('DB_HOST', 'localhost'),
  'database' => env('DB_DATABASE', 'forge'),
  'username' => env('DB_USERNAME', 'forge'),
  'password' => env('DB_PASSWORD', ''),
  'charset'  => 'utf8',
  'prefix'   => '',
],



"pure" PDO connection file that gives error:

<?php
  ini_set('display_errors', true);
  ini_set('display_startup_errors', true);
  error_reporting(E_ALL);

  try{
    $hostname = "IP, Porta";
    $dbname = "nome do banco";
    $username = "usuário do banco";
    $pw = "senha do banco";
    $pdo = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
  }catch(PDOException $e){
    echo "Erro de Conexão " . $e->getMessage() . "\n";
    exit;
  }

  $query = $pdo->prepare("SELECT * FROM dbo.USUARIOS");
  $query->execute();

  for($i=0; $row = $query->fetch(); $i++){
    echo $i." - ".$row['Coluna']."<br/>";
  }
?>
  

Error Displayed: Connection Error could not find driver.



"pure" PDO connection file that gives error:

<?php
      ini_set('display_errors', true);
      ini_set('display_startup_errors', true);
      error_reporting(E_ALL);

      try{
        $hostname = "IP, Porta";
        $dbname = "nome do banco";
        $username = "usuário do banco";
        $pw = "senha do banco";
        $pdo = new PDO ("sqlsrv:host=$hostname;dbname=$dbname","$username","$pw");
      }catch(PDOException $e){
        echo "Erro de Conexão " . $e->getMessage() . "\n";
        exit;
      }

      $query = $pdo->prepare("SELECT * FROM dbo.USUARIOS");
      $query->execute();

      for($i=0; $row = $query->fetch(); $i++){
        echo $i." - ".$row['Coluna']."<br/>";
      }
?>
  

This file where I only change mssql by sqlsrv gives the following error: Error   SQLSTATE Connection [IMSSP]: An invalid keyword 'host' was specified in   the DSN string.

UPDATE

PDO with pure php is already working, I left it equal to the call of the method used by Laravel:

<?php
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', true);
  error_reporting(E_ALL);

  try{
    // $hostname = "OLIMPO\SQLSERVER_DESENV";
    $port     = "NUMPORTA";
    $hostname = "IPDOSERVER".",".$port;
    $user     = "USERNAME";
    $senha    = "PASSWORD";
    $dbase    = "DBName";

    $pdo = new PDO("sqlsrv:Server={$hostname};Database={$dbase}", "{$user}", "{$senha}");
  }catch(PDOException $e){
    echo "Erro de Conexão " . $e->getMessage() . "\n";
    exit;
  }

  $sql = "SELECT * FROM dbo.USUARIOS";
  foreach($pdo->query($sql) as $row){
        echo "<pre>";
        print_r($row);
        echo "</pre>";
    }

  unset($pdo);
  unset($sql);
?>



  

Now is to find out what is happening in Laravel because in truth   the purpose of all this was to use Laravel instead of CodeIgniter   to develop the application:

    
asked by anonymous 16.11.2015 / 18:58

1 answer

2

Well, first you need the php_pdo_sqlsrv PDO drivers, which are not the same as php_sqlsrv . Then you have to put sqlsrv to be the driver in Lavarel.

'sqlsrv' => array(
                'driver'   => 'sqlsrv',
                'host'     => 'IPdoServidor',
                'database' => 'BancoDeDados',
                'username' => 'Usuario',
                'password' => 'Senha',
                'prefix'   => '',
            ),

In PHP.INI in the PHP folder, you have to put a new extension: php_pdo_sqlsrv_55_nts or php_pdo_sqlsrv_55_ts (where "55" refers to PHP version 5.5) p>

Make sure that the PHP ext folder has the same sqlsrv_pdo extension. If so, in php.ini point to it: extension=php_pdo_sqlsrv_55_nts.dll . If not, download the drivers , save it in the PHP ext folder and point to the drivers as in the example.

    
02.12.2015 / 23:20