程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 關於php連接mssql:pdo odbc sql server

關於php連接mssql:pdo odbc sql server

編輯:關於PHP編程

只有一個php_pdo_odbc.dll。
so~最新最好的php連接mssql方法應該是這樣:
復制代碼 代碼如下:
<?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);
}
?>


關於PHP無法連接MSSQL數據庫的問題
今天配置了新服務器,配置是IIS+php,結果運行時發現php連接遠程mssql數據庫出錯,出錯代碼如下:
Warning: mssql_connect(): Unable to connect to server:
想想以前都是沒問題的,怎麼回事呢?後來去網上搜索,發現一篇文章,才發現原來服務器是需要安裝mssql才能用php連接mssql的,本來新服務器上我是不需要用到mssql的,但是現在沒辦法了,只能把它裝上了,安裝了mssql之後就沒問題了。
我在想,如果是在linux上面的apache+php會怎樣呢,不可能需要安裝mssql吧,呵呵,暈了。
下面是找到的一篇文章。
php配置:
在php.ini文件裡設置如下,找到
;extension=php_mssql.dll 把前面的分號去掉
找到extension_dir = d:\extension\
你的php.ini裡面可能不是d:\extension\
改成在php安裝目錄下面的extensions目錄下面的php_mssql.dll,所在的路徑,如果你沒有把它移動到其他地方(假設你的php安裝路徑是d:\php)
就改成extension_dir=d:\php\extensions\
然後重新啟動web服務器

這一點很容易做到,但是做完這樣的設置後還是連不上,錯誤的信息如下:
MS SQL Server 數據庫連接錯誤!請檢查數據庫主機變量設置是否正確!!!
而主機的變量設置我是一遍一遍的檢查,那些設置是一點問題都沒有的,翻遍網頁,找到了下面的這點蛛絲馬跡:

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連接服務。
我用的php版本是4.3.9,在安裝它的服器的 windows/system32/ 下我查到ntwdblib.dll文件的版本是2000.2.8.0 ,這個版本支持的是sql server 7.0, 因為安裝PHP時會把dlls下面的所有文件覆蓋到系統
目錄下,所以當我用它去連接 sql server 2000 的時候當然會是無法連接了。
後來我在一台正常安裝sql server 2000 的服務器上查到 ntwdblib.dll的版本是 2000.80.2039.0,我把這個文件拷過去,覆蓋掉以前的版本,重啟服務器後,一切正常。
補充:如果數據庫名的開頭是數字時也會提示無法打開,這時要做的很簡單,把數據庫的名字用中括號 [ ]
括起來就搞定了,如 123bbs 改寫成 [123bbs]就沒有問題了,另外如果你的數據庫名字與sql server中的保留字沖突的話也會出現這種情況,用中括號的方法一樣可以解決。
最終,PHP無法正確連接sql server 2000的問題終於解決了,雖然耗費大半天的時間,但收獲還是很大的,現在把它貼出來,也讓遇到同樣問題的兄弟們少走一些彎路。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved