I want to connect Laravel (version 8), with a SQL Server database (SQL Server 2008 R2).
I’ve done the installation and followed some tutorials with steps like the following, where the version I installed adapts to SQL Server 2008 R2, like this guide :
-
Installed Microsoft® ODBC Driver 17 for SQL Server (I chose version 17 because of this)
-
Installed PHP Driver version 5.6 by taking 2 files, namely
php_pdo_sqlsrv_73_ts.dll
andphp_sqlsrv_73_ts.dll
which I got from here (I chose version 5.6 based on this) -
I put the above 2 files in
C:larragonbinphpphp-7.3.9-Win32-VC15-x64ext
-
I enabled the extension in
php.ini
, like this:extension=pdo_sqlsrv_73_ts
,
extension=sqlsrv_73_ts
. It succeed, When I checked inphp.info
, it sayspdo_sqlsrv
-
Then I configured the database connection on Laravel’s
.env
like this (I’ve created a database with that name):
DB_CONNECTION=sqlsrv DB_HOST=192.168.101.103:86 DB_PORT=1433 DB_DATABASE=testlaravel DB_USERNAME=faisallocal DB_PASSWORD=faisallocal
- Then I run
php artisan migrate
on terminal.
But I always get an error like this:
[IlluminateDatabaseQueryExceptioncould not find driver (SQL: select * from sys.sysobjects where id =
object_id(migrations) and xtype in (‘U’, ‘V’))at
C:larragonwwwredeempointappapivendorlaravelframeworksrcIlluminateDatabaseConnection.php:712
708▕ // If an exception occurs when trying to run a query, we’ll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database’s errors.
711▕ catch (Exception $e) { 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e 714▕ );
715▕ }
716▕ }1
C:larragonwwwredeempointappapivendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php:70
PDOException::("could not find driver")2
C:larragonwwwredeempointappapivendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php:70
PDO::__construct("dblib:host=192.168.101.103:86:1433;dbname=testlaravel;charset=utf8",
"faisallocal", "faisallocal", [])]
What did I miss?
I did this test locally, and I have XAMPP as well as Laragon with different versions, but wouldn’t it be okay if I had a different port?
Please help anyone who has experience and has advice on this, I’ve been stuck at this point for weeks..
Thank you
2
Answers
So this all happened because of the detected different versions of PHP and SQL Server drivers.
For information, I have 3 local servers, namely:
The problem that occurs is, when I want to develop apps using the Laragon server AND I use the local Terminal/Command Prompt (Shortcut: Windows + CMD), the PHP versions from other local servers will collide with each other.
When I run a Laravel command, check the PHP version, or whatever, the local Command Prompt directs me to the version of XAMPP above that I have installed, so any
.dll
or plugins you install in Laragon won't be detected!Finally I was able to finish my installation and do a lot of CMD commands according to the version I want, is how to use the built-in CMD/Terminal from Laragon (Click on Terminal under Laragon application)
From this Terminal, all the
.dll
plugins that you install, and all the cmd commands that you enter, will match the Laragon version you are using, they won't be strayed to another local server! :Dphp_pdo_sqlsrv_73_ts.dll It depends of your version of php
for example: php_pdo_sqlsrv_73_ts.dll is for php 7.3
in the next link you can see this information.
https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-ver15