13
I need a help, I made several attempts to connect Laravel with SQL Server and all without success. However, I managed to make it work with 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 networked outside of my development computer, but in any case, will print my sql server manager:
I have tried to connect only with the server name instead of "ip, port".
I tried to make a pure connect with new PDO and it did not work, the message that
returns is "could not find driver". And in Laravel the error is equal:
"Pdoexception in Connector.php line 55: could not find driver".
I did some debugging and saw that the code stops in the file "createConnection":
vendorLaravel framework src Illuminate Database Connectors Connector.php
And the code stops exactly where it tries to do 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 makes connection like this instead of per PDO??) there is some configuration error, or in . dll from the file preventing me from using the PDO.
Configuration file .env:
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
Filing cabinet database php.:
'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' => '',
],
Filing cabinet "pure" PDO connection giving 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.
Filing cabinet "pure" PDO connection giving 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 only change mssql for sqlsrv gives the following error: Error SQLSTATE[IMSSP] Connection: An invalid keyword 'host' was specified in the DSN string.
UPDATE
PDO with pure php is already working, 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’s happening in the Laravel because in fact the purpose of all this was to use Laravel instead of Codeigniter to develop the application:
Let’s go continue this discussion in chat.
– Marcos Regis