|
so~最新最好的php連接mssql方法應(yīng)該是這樣:
復(fù)制代碼 代碼如下:
<?php
$cnx = new PDO("odbc:Driver={SQL Server};Server=127.0.0.1;Database=test;",'sa','asd123');
var_dump($cnx);
$a = $cnx->query("SELECT * FROM [user]");
var_dump($a);
foreach ($a as $b) {
var_dump($b);
}
?>
關(guān)于php無法連接MSSQL數(shù)據(jù)庫的問題
今天配置了新服務(wù)器,配置是IIS+php,結(jié)果運行時發(fā)現(xiàn)php連接遠(yuǎn)程mssql數(shù)據(jù)庫出錯,出錯代碼如下:
Warning: mssql_connect(): Unable to connect to server:
想想以前都是沒問題的,怎么回事呢?后來去網(wǎng)上搜索,發(fā)現(xiàn)一篇文章,才發(fā)現(xiàn)原來服務(wù)器是需要安裝mssql才能用php連接mssql的,本來新服務(wù)器上我是不需要用到mssql的,但是現(xiàn)在沒辦法了,只能把它裝上了,安裝了mssql之后就沒問題了。
我在想,如果是在linux上面的apache+php會怎樣呢,不可能需要安裝mssql吧,呵呵,暈了。
下面是找到的一篇文章。
php配置:
在php.ini文件里設(shè)置如下,找到
;extension=php_mssql.dll 把前面的分號去掉
找到extension_dir = d:/extension/
你的php.ini里面可能不是d:/extension/
改成在php安裝目錄下面的extensions目錄下面的php_mssql.dll,所在的路徑,如果你沒有把它移動到其他地方(假設(shè)你的php安裝路徑是d:/php)
就改成extension_dir=d:/php/extensions/
然后重新啟動web服務(wù)器
這一點很容易做到,但是做完這樣的設(shè)置后還是連不上,錯誤的信息如下:
MS SQL Server 數(shù)據(jù)庫連接錯誤!請檢查數(shù)據(jù)庫主機變量設(shè)置是否正確!!!
而主機的變量設(shè)置我是一遍一遍的檢查,那些設(shè)置是一點問題都沒有的,翻遍網(wǎng)頁,找到了下面的這點蛛絲馬跡:
php.com資料:
I am trying to connect to SQL Server 2000 from php
I bumped to following warning:
Warning: mssql_connect(): Unable to connect to server: SERVER/Portal
....... on line 5
on line 5 there is:
$db_connect = mssql_connect('SERVER/Portal', 'sa', 'my_passwd');
I did the following
1.enabled php_mssql.dll extension in php.ini
2.every dll is in proper place(System32 or php folder),including ntwdblib.dll
I search lots of profile throught web ,but no one give me proper answer to resolve it.
after a few hour ,I found the problem was caused by
ntwdblib.dll ,which version is 7.00.839 ,when I replaced old ntwdblib.dll with the new
ntwdblib.dll ,which version is 8.00.194 ,all problem are solved.
We had some, read A LOT, of problems with MSSQL under Windows 2003.
We had 2 the same windows, php, php-ini, everything machines but only one could connect.
Unable to connect was the error message.
Finnaly we checked the version of ntwdblib.dll and the one distributed with php was 7.00....
and the version of the one on the SQL Server install was 8.00.... so we copied this one in
the php and apache dir and it worked.
問題就這樣被找到了,惹禍的是它 ntwdblib.dll
ntwdblib.dll的主要作用是提供sql server連接服務(wù)。
我用的php版本是4.3.9,在安裝它的服器的 windows/system32/ 下我查到ntwdblib.dll文件的版本是2000.2.8.0 ,這個版本支持的是sql server 7.0, 因為安裝php時會把dlls下面的所有文件覆蓋到系統(tǒng)
目錄下,所以當(dāng)我用它去連接 sql server 2000 的時候當(dāng)然會是無法連接了。
后來我在一臺正常安裝sql server 2000 的服務(wù)器上查到 ntwdblib.dll的版本是 2000.80.2039.0,我把這個文件拷過去,覆蓋掉以前的版本,重啟服務(wù)器后,一切正常。
補充:如果數(shù)據(jù)庫名的開頭是數(shù)字時也會提示無法打開,這時要做的很簡單,把數(shù)據(jù)庫的名字用中括號 [ ]
括起來就搞定了,如 123bbs 改寫成 [123bbs]就沒有問題了,另外如果你的數(shù)據(jù)庫名字與sql server中的保留字沖突的話也會出現(xiàn)這種情況,用中括號的方法一樣可以解決。
最終,php無法正確連接sql server 2000的問題終于解決了,雖然耗費大半天的時間,但收獲還是很大的,現(xiàn)在把它貼出來,也讓遇到同樣問題的兄弟們少走一些彎路。
php技術(shù):關(guān)于php連接mssql:pdo odbc sql server,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。