Laravel 和 MS SQL 服务器数据库连接抛出错误“(3/3) QueryException 找不到驱动程序”
Laravel and MS SQL Server Database connection is throwing the error "(3/3) QueryException could not find driver"
Guys, I tried lots of things mentioned in Whosebug and laracasts for this issue but none worked, hence posting this in hope of getting some answer or advice.
此外,提供的大部分内容属于 MYSQL 不适用于 MS SQL。
我正在使用 Windows 10 并使用 Laravel 连接到我的 Microsoft SQL Server 2012 Express 版。
我有如下 .erv 文件:
DB_CONNECTION=sqlsrv
DB_HOST=W1234567\SQLEXPRESS
DB_PORT=1433
DB_DATABASE=My_Database_Name
DB_USERNAME=sa
DB_PASSWORD=My_Password
给定的详细信息是正确的,但是当我 运行 进行简单查询时,我在 Laravel Model
:
中收到以下错误消息
(3/3) QueryException
could not find driver (SQL: SELECT COUNT(*) AS count FROM TBL_Data
WHERE ACTIVE_IND = 1
)
我正确地安装了 WAMP 运行,我已将 SQLSRV
文件添加到我的 WAMP。
整个工作正常,现在我将代码移到不同的系统那里我收到此错误(两个系统具有相同的凭据,唯一的变化是我更改的服务器名称)所以代码应该可以工作正确,因为没有完成任何更改。
我尝试检查 SQL Server Configuration Manager
中的 SQL 服务器 TCP/IP 端口号并将其更改为 1433 仍然没有成功。
我检查了这里的大部分答案,它们属于 PDO 异常,并要求人们取消注释部分 extension=php_pdo_mysql.dll
,但在我的情况下,它的 SQL 服务器和普通 wamp 工作正常似乎只是LARAVEL.
有问题
任何人都可以提供解决方案吗?
Edited part below with some more info:
我检查了给定的凭据是否正确,以及我是否能够使用控制器中的一个简单函数连接到数据库:
我的路线:Route::get('/Test', 'My_Controller@Test');
我的控制器:
public function Test(Request $request)
{
if(DB::connection()->getDatabaseName())
{
echo "connected successfully to database ".DB::connection()->getDatabaseName();
}
}
Chromewindow中的输出:connected successfully to database My_Database_Name
正如我们从上面的消息中看到的,我能够正确连接但是当我 运行 查询时我仍然收到错误:
(3/3) QueryException
could not find driver (SQL: SELECT * FROM TBL_Data WHERE ACTIVE_IND = 1 )
在控制器中添加 phpinfo();
并尝试点击 URL 并获得以下与 PDO 相关的信息:
PDO support enabled
PDO drivers mysql, SQLite
SQL此处未启用 SRV,这是导致问题的原因吗?
This did NOT work for me but may work for some people hence posting this answer, hope this will be helpful
我试过更改 SQL 服务器的端口号,但这个还是不行,如果有人想试试,可以试试这个,看看这个能不能用。
The issue was with MS-SQL Server 2012 Express didn't have port 1433 in the SQL Server Configuration Manager > SQL Server Network Configuration > TCP/IP > Properties> TCP Port was BLANK! Just type 1433 and restart the services and BAM!! Like Magic!
所以我终于弄清楚了这个问题,发布了同样的问题,这样它会帮助那些已经罢工几天并撕破头发的人找到解决方案。
确保在 Laravel 连接时提供了正确的端口,并且与 SQL Server Configuration Manager
TCP/IP.
确保您的 TCP/IP 在 SQL Server Configuration Manager
中启用。
我的 php.ini
文件中缺少一些 Extension
,所以我将这些扩展复制粘贴到 php.ini
文件中的 PHP 的 WAMP。
分机=php_bz2.dll
分机=php_curl.dll
分机=php_sqlsrv_56_ts.dll
分机=php_sqlsrv_56_nts.dll
分机=php_com_dotnet.dll
;分机=php_enchant.dll
分机=php_fileinfo.dll
;分机=php_ftp.dll
分机=php_gd2.dll
分机=php_gettext.dll
分机=php_gmp.dll
分机=php_intl.dll
分机=php_imap.dll
;分机=php_interbase.dll
分机=php_ldap.dll
分机=php_mbstring.dll
分机=php_exif.dll;必须在 mbstring 之后,因为它取决于它
分机=php_mysqli.dll
分机=php_odbc.dll
分机=php_openssl.dll
;分机=php_pdo_firebird.dll
分机=php_pdo_mysql.dll
;分机=php_pdo_oci.dll
;分机=php_oci8_12c.dll;与 Oracle Database 12c 即时客户端一起使用
分机=php_pdo_odbc.dll
;分机=php_pdo_pgsql.dll
分机=php_pdo_sqlite.dll
;分机=php_pgsql.dll
;分机=php_phpdbg_webhelper.dll
;分机=php_shmop.dll
每次在 Laravel 中的 .env
文件中更改内容时,您需要刷新它,否则它仍将使用您提供的旧数据。
php artisan config:clear
php artisan config:cache
重新启动 WAMP 服务器和 Laravel 服务器 php artisan server
我的.env文件在Laravel:
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=DataBase_Name
DB_USERNAME=sa
DB_PASSWORD=MyPass
希望这对你们有用,否则一切顺利并尝试找到解决方案,也请告诉我。
检查以下位置的 php.ini
文件 C:\wamp\bin\php\php5.6.35\php.ini
并添加以下扩展名(如果不存在)。
extension=php_pdo_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_nts.dll
这也将解决问题。
可能有点晚了,但这是 PHP + SqlServer
的 docker 文件
FROM php:7.4.4-fpm-alpine as base
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions sqlsrv pdo_sqlsrv
# Sql server necessary additions
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.1.1-1_amd64.apk \
&& curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.1.2-1_amd64.apk \
&& apk add --allow-untrusted msodbcsql17_17.5.1.1-1_amd64.apk \
&& apk add --allow-untrusted mssql-tools_17.5.1.2-1_amd64.apk
# Fix for sql server
ENV LC_ALL=C
Guys, I tried lots of things mentioned in Whosebug and laracasts for this issue but none worked, hence posting this in hope of getting some answer or advice.
此外,提供的大部分内容属于 MYSQL 不适用于 MS SQL。
我正在使用 Windows 10 并使用 Laravel 连接到我的 Microsoft SQL Server 2012 Express 版。
我有如下 .erv 文件:
DB_CONNECTION=sqlsrv
DB_HOST=W1234567\SQLEXPRESS
DB_PORT=1433
DB_DATABASE=My_Database_Name
DB_USERNAME=sa
DB_PASSWORD=My_Password
给定的详细信息是正确的,但是当我 运行 进行简单查询时,我在 Laravel Model
:
(3/3) QueryException
could not find driver (SQL: SELECT COUNT(*) AS count FROM TBL_Data
WHERE ACTIVE_IND = 1
)
我正确地安装了 WAMP 运行,我已将 SQLSRV
文件添加到我的 WAMP。
整个工作正常,现在我将代码移到不同的系统那里我收到此错误(两个系统具有相同的凭据,唯一的变化是我更改的服务器名称)所以代码应该可以工作正确,因为没有完成任何更改。
我尝试检查 SQL Server Configuration Manager
中的 SQL 服务器 TCP/IP 端口号并将其更改为 1433 仍然没有成功。
我检查了这里的大部分答案,它们属于 PDO 异常,并要求人们取消注释部分 extension=php_pdo_mysql.dll
,但在我的情况下,它的 SQL 服务器和普通 wamp 工作正常似乎只是LARAVEL.
任何人都可以提供解决方案吗?
Edited part below with some more info:
我检查了给定的凭据是否正确,以及我是否能够使用控制器中的一个简单函数连接到数据库:
我的路线:Route::get('/Test', 'My_Controller@Test');
我的控制器:
public function Test(Request $request)
{
if(DB::connection()->getDatabaseName())
{
echo "connected successfully to database ".DB::connection()->getDatabaseName();
}
}
Chromewindow中的输出:connected successfully to database My_Database_Name
正如我们从上面的消息中看到的,我能够正确连接但是当我 运行 查询时我仍然收到错误:
(3/3) QueryException
could not find driver (SQL: SELECT * FROM TBL_Data WHERE ACTIVE_IND = 1 )
在控制器中添加 phpinfo();
并尝试点击 URL 并获得以下与 PDO 相关的信息:
PDO support enabled
PDO drivers mysql, SQLite
SQL此处未启用 SRV,这是导致问题的原因吗?
This did NOT work for me but may work for some people hence posting this answer, hope this will be helpful
我试过更改 SQL 服务器的端口号,但这个还是不行,如果有人想试试,可以试试这个,看看这个能不能用。
The issue was with MS-SQL Server 2012 Express didn't have port 1433 in the SQL Server Configuration Manager > SQL Server Network Configuration > TCP/IP > Properties> TCP Port was BLANK! Just type 1433 and restart the services and BAM!! Like Magic!
所以我终于弄清楚了这个问题,发布了同样的问题,这样它会帮助那些已经罢工几天并撕破头发的人找到解决方案。
确保在 Laravel 连接时提供了正确的端口,并且与
SQL Server Configuration Manager
TCP/IP.确保您的 TCP/IP 在
SQL Server Configuration Manager
中启用。我的
php.ini
文件中缺少一些Extension
,所以我将这些扩展复制粘贴到php.ini
文件中的 PHP 的 WAMP。分机=php_bz2.dll
分机=php_curl.dll
分机=php_sqlsrv_56_ts.dll
分机=php_sqlsrv_56_nts.dll
分机=php_com_dotnet.dll
;分机=php_enchant.dll
分机=php_fileinfo.dll
;分机=php_ftp.dll
分机=php_gd2.dll
分机=php_gettext.dll
分机=php_gmp.dll
分机=php_intl.dll
分机=php_imap.dll
;分机=php_interbase.dll
分机=php_ldap.dll
分机=php_mbstring.dll
分机=php_exif.dll;必须在 mbstring 之后,因为它取决于它
分机=php_mysqli.dll
分机=php_odbc.dll
分机=php_openssl.dll
;分机=php_pdo_firebird.dll
分机=php_pdo_mysql.dll
;分机=php_pdo_oci.dll
;分机=php_oci8_12c.dll;与 Oracle Database 12c 即时客户端一起使用
分机=php_pdo_odbc.dll
;分机=php_pdo_pgsql.dll
分机=php_pdo_sqlite.dll
;分机=php_pgsql.dll
;分机=php_phpdbg_webhelper.dll
;分机=php_shmop.dll
每次在 Laravel 中的
.env
文件中更改内容时,您需要刷新它,否则它仍将使用您提供的旧数据。php artisan config:clear
php artisan config:cache
重新启动 WAMP 服务器和 Laravel 服务器
php artisan server
我的.env文件在Laravel:
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=DataBase_Name
DB_USERNAME=sa
DB_PASSWORD=MyPass
希望这对你们有用,否则一切顺利并尝试找到解决方案,也请告诉我。
检查以下位置的 php.ini
文件 C:\wamp\bin\php\php5.6.35\php.ini
并添加以下扩展名(如果不存在)。
extension=php_pdo_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_nts.dll
这也将解决问题。
可能有点晚了,但这是 PHP + SqlServer
的 docker 文件FROM php:7.4.4-fpm-alpine as base
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions sqlsrv pdo_sqlsrv
# Sql server necessary additions
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.1.1-1_amd64.apk \
&& curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.1.2-1_amd64.apk \
&& apk add --allow-untrusted msodbcsql17_17.5.1.1-1_amd64.apk \
&& apk add --allow-untrusted mssql-tools_17.5.1.2-1_amd64.apk
# Fix for sql server
ENV LC_ALL=C