Invalid Handle returned - Laravel connection to SQL Server

Asked

Viewed 750 times

2

I was developing an application with Laravel 5.5 with a Mysql database. Everything was working normally, however, now the database has been modified to SQL Server.

When I perform the test, returns error "Invalid Handle returned."

  • Searching, I saw that could be the version of my php, so I changed it, I was using 7.1 and now I am in 7.0.23
  • All drivers (including pdo_sqlsvr) are downloaded and set correctly (php.ini and ext folder)
  • Microsoft ODBC installed
  • All sql server firewall rules are configured and released

My file . env is set the following:

APP_NAME=Incorptech
APP_ENV=local
APP_KEY=[key da aplicação está ok]
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=sqlsrv
DB_HOST=[host está ok]
DB_PORT=1433
DB_DATABASE=[nome do banco]
DB_USERNAME=[usuario do banco]
DB_PASSWORD=[senha do banco]

The database.php is with the following settings:

'default' => env('DB_CONNECTION', 'sqlsrv'),
  'sqlsrv' => [
  'driver' => 'sqlsrv',
  'host' => env('DB_HOST', 'localhost'),
  'port' => env('DB_PORT', '1433'),
  'database' => env('DB_DATABASE', 'nomedobanco'),
  'username' => env('DB_USERNAME', 'usuario'),
  'password' => env('DB_PASSWORD', 'senha'),
  'charset' => 'utf8',
  'prefix' => '',
],

The function that I am performing the test and that this error is coming back, is the following:

public function testeDB() {
  $select = DB::select('select * from conta');
  return $select;
}    

In the Artisan command prompt, the following error appears:

PHP Fatal error: Invalid Handle returned in ...vendor framework src Illuminate Database Connectors Connector.php on line 67

The function of line 67 of the above error (this function is of the proper Standard), in the file \Connectors\Connector.php is as follows:

protected function createPdoConnection($dsn, $username, $password, 
    $options) {
  if (class_exists(PDOConnection::class) && ! $this->isPersistentConnection($options)) {
    return new PDOConnection($dsn, $username, $password, $options);
  }
  return new PDO($dsn, $username, $password, $options);
}

I started developing in Laravel a little while ago, there is something else to be created for this connection with SQL Server to be successfully performed?

  • This may help: https://answall.com/questions/98544/laravel-5-e-sql-server

  • Hey, Miguel, what’s up? Before posting, I researched and looked yes this article, but in my case, I have no pure php class creating this PDO connection. My doubt is just this, with Mysql, I didn’t have to do any of this manually, but with SQL Server I’m a little lost

  • I have no experience with this problem, I can’t help you from my own experience because I never connected to this database. But there are a number of links by google that help you, e.g.: https://stackoverflow.com/questions/36428531/laravel-5-2-connect-to-ms-sql-2012

1 answer

2

I ended up solving the problem.

In SQL Server Configuration manager, under "SQL Server Network Configuration", the TCP/IP protocol was enabled, but in the "IP Addresses" tab, the port field was empty. When I filled in port 1433, the problem was solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.