如何通过PHP和Linux使用pdo连接mssql?

发布于 2024-11-06 09:48:01 字数 1061 浏览 1 评论 0原文

我正在尝试使用以下代码建立新的 PDO 连接。

new PDO("mssql:driver=????;Server={$serverName};Database={$databaseName}", $username, $password, array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

我不确定使用什么驱动程序?或者如何安装它们。我可以使用 PHP 中的 mssql_connect 函数完美连接,但我想使用 PDO 库。

我的 mssql php.ini 设置是:

ssql

MSSQL Support   enabled
Active Persistent Links     0
Active Links    1
Library version     FreeTDS

Directive   Local Value Master Value
mssql.allow_persistent  On  On
mssql.batchsize 0   0
mssql.charset   no value    no value
mssql.compatability_mode    Off Off
mssql.connect_timeout   5   5
mssql.datetimeconvert   On  On
mssql.max_links Unlimited   Unlimited
mssql.max_persistent    Unlimited   Unlimited
mssql.max_procs Unlimited   Unlimited
mssql.min_error_severity    10  10
mssql.min_message_severity  10  10
mssql.secure_connection Off Off
mssql.textlimit Server default  Server default
mssql.textsize  Server default  Server default
mssql.timeout   60  60

I'm trying to for a new PDO connection using the following code.

new PDO("mssql:driver=????;Server={$serverName};Database={$databaseName}", $username, $password, array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

I'm not sure what drivers to use? Or how to install them. I can connect perfectly fine using the mssql_connect function in PHP but I want to use the PDO library instead.

My php.ini settings for mssql are:

ssql

MSSQL Support   enabled
Active Persistent Links     0
Active Links    1
Library version     FreeTDS

Directive   Local Value Master Value
mssql.allow_persistent  On  On
mssql.batchsize 0   0
mssql.charset   no value    no value
mssql.compatability_mode    Off Off
mssql.connect_timeout   5   5
mssql.datetimeconvert   On  On
mssql.max_links Unlimited   Unlimited
mssql.max_persistent    Unlimited   Unlimited
mssql.max_procs Unlimited   Unlimited
mssql.min_error_severity    10  10
mssql.min_message_severity  10  10
mssql.secure_connection Off Off
mssql.textlimit Server default  Server default
mssql.textsize  Server default  Server default
mssql.timeout   60  60

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆那么伤 2024-11-13 09:48:01

PDO mssql驱动程序不再存在,使用sqlsrv(在php windows下)或dblib(在php linux下)

http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

http://www.php.net/manual/en/ref.pdo-dblib.php

The PDO mssql driver is no more, use sqlsrv (under php windows) or dblib (under php linux)

http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

http://www.php.net/manual/en/ref.pdo-dblib.php

森罗 2024-11-13 09:48:01

我运行的是 Ubuntu 14.04。尝试连接到 MSSQL 时,我收到“未捕获的异常‘PDOException’,消息为‘无法找到驱动程序’”。看来我缺少 dblib/sybase PHP 扩展。

我不得不跑:

sudo apt-get install php5-sybase freetds-common libsybdb5 \ 
&& sudo apache2ctl restart

现在工作正常。

I am running Ubuntu 14.04. Trying to connect to MSSQL I got "Uncaught exception 'PDOException' with message 'could not find driver'". It seems that I was missing the dblib/sybase PHP extension.

I had to run:

sudo apt-get install php5-sybase freetds-common libsybdb5 \ 
&& sudo apache2ctl restart

Works fine now.

壹場煙雨 2024-11-13 09:48:01

尝试

$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");

$hostname may need to be configured as either...
$hostname.':'.$port;

$hostname.','.$port;

Try

$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");

$hostname may need to be configured as either...
$hostname.':'.$port;

OR

$hostname.','.$port;
吃兔兔 2024-11-13 09:48:01

Karl Wilbur 发布的 dblib/sybase PHP 扩展技巧对我有用。
LimeSurvey 的预安装检查页面现在显示

PHP PDO 驱动程序库 - Microsoft SQL Server (dblib)、MySQL

只需确保找到并安装与您正在使用的 PHP 版本一致的版本即可;

提示>sudo apt-get install php<合适的版本>-sybase freetds-common libsybdb5
提示>sudo apache2ctl restart

干杯,

The dblib/sybase PHP extension tip posted by Karl Wilbur worked for me.
The pre-installation check page for LimeSurvey now shows

PHP PDO driver library- Microsoft SQL Server (dblib), MySQL

Just make sure you find and install the version that aligns with the PHP version you are using;

Prompt>sudo apt-get install php<appropriate version>-sybase freetds-common libsybdb5
Prompt>sudo apache2ctl restart

Cheers,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文