問題描述
我有一臺 Linux 服務(wù)器 Debian 6,安裝了 Apache 2.2 和 PHP 5.4.我需要將我的應(yīng)用程序與 MS SQL Server 2008 連接.
I have a Linux server Debian 6, with Apache 2.2 and PHP 5.4 installed. I need to connect my application with a MS SQL Server 2008.
我的應(yīng)用程序使用 Zend Framework 1.11 和字符集 UTF-8(我將擁有來自世界各地的用戶,他們將使用自己的語言放置數(shù)據(jù)).
My application is using Zend Framework 1.11 and charset UTF-8 (I'll have users from all places in the world and they will put data in their own language).
首先,我嘗試將 Microsoft SQL Server ODBC 驅(qū)動程序用于 Linux.它說僅適用于 Red Hat,但我按照以下說明進(jìn)行安裝:
FRIST, I tried to use Microsoft SQL Server ODBC driver for Linux. It says is only for Red Hat, but I follow these instructions to install:
http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/
我可以連接并對其進(jìn)行一些選擇,但無法在其上插入數(shù)據(jù).我在 pdo 語句上綁定參數(shù)時(shí)遇到問題.
I could connect and make some selects on it, but I couldn't insert data on it. I got a problem on binding parameters on pdo statements.
插入如下數(shù)據(jù)無效:
$stmt = $conn->prepare("insert into mar_regions (name) values (:name)");
$resp = $stmt->execute(array(':name' => $param));
但是如果我像這樣使用它,它會起作用:
But if I used like the this, it works:
$stmt = $conn->prepare("insert into mar_regions (name) values ('".$param."')");
$resp = $stmt->execute();
所以我放棄了這個(gè)驅(qū)動程序,因?yàn)槿绻@樣的話,我的應(yīng)用程序沒有 ZF 1.11 將無法工作.
So I gave up from this driver, because my application no ZF 1.11 will not work if this.
第二,我嘗試將 PDO 驅(qū)動程序用于 FreeTDS.這個(gè)工作正常,我可以在我的 ZF 1.11 應(yīng)用程序中使用.
SECOND, I try to use PDO Driver for FreeTDS. This one works ok and I could use on my ZF 1.11 application.
但是,我又遇到了一個(gè)問題:字符集.我將我的 freeTDS.conf 配置為使用 UTF-8,將我的表更改為使用 NVARCHAR insted of VARCHAR,并且可以像這樣插入 utf-8 數(shù)據(jù):
But then, I got one more problem: charsets. I configure my freeTDS.conf to use UTF-8, change my tables to use NVARCHAR insted of VARCHAR and could insert utf-8 data like this:
$stmt = $dbh->prepare("insert into mar_teste (name) values (N'ンから初?配信 € зеленый банан ààààáááááá')");
$resp = $stmt->execute();
但是,在我的 ZF 1.11 上,我無法在查詢中傳遞這個(gè)N"屬性!所以我的應(yīng)用程序仍然沒有工作.
But, on my ZF 1.11, I can't pass this 'N' attribute on querys! So my application still didn't work.
如你所見,我什么都試過了.
As you can see I tried everything.
所以我的問題是:如何在 MS SQL Server 2008 上使用 ZF 1.11 字符集 UTF-8 從 linux 連接?
So my question is: How to connect from linux, using ZF 1.11 charset UTF-8, on MS SQL Server 2008?
推薦答案
我的問題的答案是:使用 freeTDS!上面有一個(gè)字符集參數(shù):
The answer for my question is: Use freeTDS! Theres a parameter for charset on it:
[MyDSN]
host = <<ip>>
port = <<port>>
# use 8.0 for newer versions of SQLSERVER
tds version = 8.0
# text size don't need to be such a high value, its just an example
text size = 4294967295
client charset = UTF-8
在 Zend Framework 上,像這樣配置您的連接:
On Zend Framework, configure your connection like this:
;; BANCO DE DADOS LINUX
database.adapter = PDO_MSSQL
database.params.pdoType = dblib
database.params.host = MyDSN
database.params.dbname = <<dbname>>
database.params.username = <<username>>
database.params.password = <<passwd>>
database.params.driver_options.charset = UTF-8
database.isDefaultTableAdapter = true
它解決了問題!;)
這篇關(guān)于Linux 上的 PHP 5.4:如何連接 MS SQL Server 2008?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!