SQL Server PDO找不到驱动程序 [英] SQL Server PDO could not find driver

查看:121
本文介绍了SQL Server PDO找不到驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为数据库使用Microsoft Azure SQL,并尝试使用XAMPP将其连接到本地主机上的项目.当我尝试使用连接字符串连接到数据库时,它们提供了:

I'm using Microsoft Azure SQL for my database and I'm trying to connect it to my project on my localhost using XAMPP. When I try to connect to the database using the connect string they provided:

    try {
    $conn = new PDO("sqlsrv:server = tcp:app.database.windows.net,1433; Database = mydatabase", "{myusername}", "{your_password_here}");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
    print("Error connecting to SQL Server.");
    die(print_r($e));
}

我不断收到此错误:

Error connecting to SQL Server.PDOException Object ( [message:protected] => could not find driver

错误的时间更长,但我认为它与最后提到的驱动程序有关.我使用的是Mac,因此我需要安装的任何驱动程序都必须与其兼容.谢谢您的提前帮助.

The error is longer but I think it's related to the driver mentioned at the end. I'm using a mac so any driver I need to install would need to be compatible with it. Thank you for the help in advance.

推荐答案

Azure SQL数据库基于Microsoft SQL Server引擎构建.因此,您可以考虑使用 pdo-dblib 在Mac上使用PDO连接到SQL Server.您可以查看堆栈关于此的溢出问题,或参考

Azure SQL Database is built on the Microsoft SQL Server engine. So you might consider using pdo-dblib to connect to SQL Server using the PDO on a Mac. You can check out a Stack Overflow question about it here or refer to this setup documentation.

在安装驱动程序之后,Azure SQL实际上还不会接受来自您本地的连接.并且您可能会收到以下错误:

After the driver having been installed, Azure SQL won’t actually accept connection from your local yet. And you might get the following error:

IP地址为167.xxx.xxx.xxx的客户端不允许访问 服务器.

Client with IP address '167.xxx.xxx.xxx' is not allowed to access the server.

这是因为默认情况下存在防火墙.要启用访问权限,请转到 Azure门户网站,点击所有资源,选择您的SQL服务,然后在设置"菜单中单击防火墙.

That’s because there is a firewall in the way by default. To enable access, go to the Azure portal, click on All Resources, select your SQL service, click on Firewall in the SETTING menu.

您的客户端地址很方便地包含在列表中,因此您只需单击添加客户端IP ,然后单击保存.

Your client address is conveniently included in the list, so you can just click on Add client IP followed by Save.

好吧,现在运行代码时,它应该可以连接.

Well, when you run your code now, it should connect.

这篇关于SQL Server PDO找不到驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆