問題描述
如果你在谷歌上搜索這個問題,你會發現很多不正確、誤導性和過時的信息.令人驚訝的是,Stack Overflow 上沒有可靠的答案,所以我們應該改變它.
If you search Google for this question, you will find a lot of incorrect, misleading, and outdated information. Surprisingly, there isn't a solid answer on Stack Overflow, so we should change that.
我使用的是 Mac 端口安裝的 Apache 和 PHP.我已經安裝了 php5-mssql,我可以在我的 phpinfo() 頁面上看到 mssql.
I am using the Mac port installation of Apache and PHP. I have installed php5-mssql, and I can see mssql on my phpinfo() page.
但我沒有看到它列在 PDO 下.
But I don't see it listed under PDO.
PDO support enabled
PDO drivers dblib, mysql, odbc, pgsql
mssql 與 PDO 無關嗎?是否有其他驅動程序可以在 Mac 上使用 PDO 連接到 SqlServer 數據庫?看起來這應該是可能的.
Is mssql not associated with PDO? Is there another driver that can be used on a Mac to connect to a SqlServer database using PDO? Seems like this is something that should be possible.
推薦答案
這對您有幫助嗎?
http://blog.nguyenvq.com/2010/05/16/freetds-unixodbc-rodbc-r/
我使用 FreeTDS 從 Linux 服務器連接到 Microsoft SQL 服務器,看起來上面鏈接中的人使用 FreeTDS 從 Mac 連接.
I use FreeTDS to connect to Microsoft SQL servers from a Linux server and it looks like the person in the link above has used FreeTDS to connect from a Mac.
這是我的/etc/freetds/freetds.conf 文件(我添加的唯一部分是在 XYZ 服務器的最后):
Here is my /etc/freetds/freetds.conf file (the only part I added was at the very end for the XYZ server):
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[xyz]
host = xyz
port = 1433
tds version = 8.0
[由提問者編輯]
FreeTDS 配置是答案的前半部分.配置完成后,您應該能夠從命令行運行類似的操作并進行連接:
FreeTDS configuration is the first half of the answer. Once it's configured you should be able to run something like this from the command line and connect:
tsql -S xyz -U username -P password
那么你需要使用dblib,而不是mssql作為PDO驅動:
Then you need to use dblib, not mssql, as the PDO driver:
$pdo = new PDO("dblib:host=$dbhost;dbname=$dbname",
"$dbuser","$dbpwd");
其中 $dbhost 是 freetds.conf 文件中的名稱
Where $dbhost is the name from the freetds.conf file
這篇關于如何使用 PHP PDO 從 Mac 連接到 Sql Server?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!