關(guān)于php連接mssql:pdo odbc sql server
更新時(shí)間:2011年07月20日 00:39:33 作者:
研究了很久,終于發(fā)現(xiàn):最新的php 5.3.6中php_mssql.dll,php_pdo_mssql.dll都已經(jīng)不見了。
只有一個(gè)php_pdo_odbc.dll。
so~最新最好的php連接mssql方法應(yīng)該是這樣:
<?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ù)庫(kù)的問題
今天配置了新服務(wù)器,配置是IIS+php,結(jié)果運(yùn)行時(shí)發(fā)現(xiàn)php連接遠(yuǎn)程mssql數(shù)據(jù)庫(kù)出錯(cuò),出錯(cuò)代碼如下:
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會(huì)怎樣呢,不可能需要安裝mssql吧,呵呵,暈了。
下面是找到的一篇文章。
php配置:
在php.ini文件里設(shè)置如下,找到
;extension=php_mssql.dll 把前面的分號(hào)去掉
找到extension_dir = d:\extension\
你的php.ini里面可能不是d:\extension\
改成在php安裝目錄下面的extensions目錄下面的php_mssql.dll,所在的路徑,如果你沒有把它移動(dòng)到其他地方(假設(shè)你的php安裝路徑是d:\php)
就改成extension_dir=d:\php\extensions\
然后重新啟動(dòng)web服務(wù)器
這一點(diǎn)很容易做到,但是做完這樣的設(shè)置后還是連不上,錯(cuò)誤的信息如下:
MS SQL Server 數(shù)據(jù)庫(kù)連接錯(cuò)誤!請(qǐng)檢查數(shù)據(jù)庫(kù)主機(jī)變量設(shè)置是否正確!!!
而主機(jī)的變量設(shè)置我是一遍一遍的檢查,那些設(shè)置是一點(diǎn)問題都沒有的,翻遍網(wǎng)頁(yè),找到了下面的這點(diǎn)蛛絲馬跡:
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 ,這個(gè)版本支持的是sql server 7.0, 因?yàn)榘惭bPHP時(shí)會(huì)把dlls下面的所有文件覆蓋到系統(tǒng)
目錄下,所以當(dāng)我用它去連接 sql server 2000 的時(shí)候當(dāng)然會(huì)是無法連接了。
后來我在一臺(tái)正常安裝sql server 2000 的服務(wù)器上查到 ntwdblib.dll的版本是 2000.80.2039.0,我把這個(gè)文件拷過去,覆蓋掉以前的版本,重啟服務(wù)器后,一切正常。
補(bǔ)充:如果數(shù)據(jù)庫(kù)名的開頭是數(shù)字時(shí)也會(huì)提示無法打開,這時(shí)要做的很簡(jiǎn)單,把數(shù)據(jù)庫(kù)的名字用中括號(hào) [ ]
括起來就搞定了,如 123bbs 改寫成 [123bbs]就沒有問題了,另外如果你的數(shù)據(jù)庫(kù)名字與sql server中的保留字沖突的話也會(huì)出現(xiàn)這種情況,用中括號(hào)的方法一樣可以解決。
最終,PHP無法正確連接sql server 2000的問題終于解決了,雖然耗費(fèi)大半天的時(shí)間,但收獲還是很大的,現(xiàn)在把它貼出來,也讓遇到同樣問題的兄弟們少走一些彎路。
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ù)庫(kù)的問題
今天配置了新服務(wù)器,配置是IIS+php,結(jié)果運(yùn)行時(shí)發(fā)現(xiàn)php連接遠(yuǎn)程mssql數(shù)據(jù)庫(kù)出錯(cuò),出錯(cuò)代碼如下:
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會(huì)怎樣呢,不可能需要安裝mssql吧,呵呵,暈了。
下面是找到的一篇文章。
php配置:
在php.ini文件里設(shè)置如下,找到
;extension=php_mssql.dll 把前面的分號(hào)去掉
找到extension_dir = d:\extension\
你的php.ini里面可能不是d:\extension\
改成在php安裝目錄下面的extensions目錄下面的php_mssql.dll,所在的路徑,如果你沒有把它移動(dòng)到其他地方(假設(shè)你的php安裝路徑是d:\php)
就改成extension_dir=d:\php\extensions\
然后重新啟動(dòng)web服務(wù)器
這一點(diǎn)很容易做到,但是做完這樣的設(shè)置后還是連不上,錯(cuò)誤的信息如下:
MS SQL Server 數(shù)據(jù)庫(kù)連接錯(cuò)誤!請(qǐng)檢查數(shù)據(jù)庫(kù)主機(jī)變量設(shè)置是否正確!!!
而主機(jī)的變量設(shè)置我是一遍一遍的檢查,那些設(shè)置是一點(diǎn)問題都沒有的,翻遍網(wǎng)頁(yè),找到了下面的這點(diǎn)蛛絲馬跡:
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 ,這個(gè)版本支持的是sql server 7.0, 因?yàn)榘惭bPHP時(shí)會(huì)把dlls下面的所有文件覆蓋到系統(tǒng)
目錄下,所以當(dāng)我用它去連接 sql server 2000 的時(shí)候當(dāng)然會(huì)是無法連接了。
后來我在一臺(tái)正常安裝sql server 2000 的服務(wù)器上查到 ntwdblib.dll的版本是 2000.80.2039.0,我把這個(gè)文件拷過去,覆蓋掉以前的版本,重啟服務(wù)器后,一切正常。
補(bǔ)充:如果數(shù)據(jù)庫(kù)名的開頭是數(shù)字時(shí)也會(huì)提示無法打開,這時(shí)要做的很簡(jiǎn)單,把數(shù)據(jù)庫(kù)的名字用中括號(hào) [ ]
括起來就搞定了,如 123bbs 改寫成 [123bbs]就沒有問題了,另外如果你的數(shù)據(jù)庫(kù)名字與sql server中的保留字沖突的話也會(huì)出現(xiàn)這種情況,用中括號(hào)的方法一樣可以解決。
最終,PHP無法正確連接sql server 2000的問題終于解決了,雖然耗費(fèi)大半天的時(shí)間,但收獲還是很大的,現(xiàn)在把它貼出來,也讓遇到同樣問題的兄弟們少走一些彎路。
您可能感興趣的文章:
- PHP5中使用PDO連接數(shù)據(jù)庫(kù)的方法
- php中mysql連接方式PDO使用詳解
- php使用pdo連接sqlserver示例分享
- php使用pdo連接mssql server數(shù)據(jù)庫(kù)實(shí)例
- php中數(shù)據(jù)庫(kù)連接方式pdo和mysqli對(duì)比分析
- php使用pdo連接sqlite3的配置示例
- PHP使用PDO連接ACCESS數(shù)據(jù)庫(kù)
- php基于PDO連接MSSQL示例DEMO
- php PDO判斷連接是否可用的實(shí)現(xiàn)方法
- PHP實(shí)現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對(duì)象關(guān)系數(shù)據(jù)庫(kù)示例
- PHP使用PDO 連接與連接管理操作實(shí)例分析
相關(guān)文章
關(guān)于PHP session 存儲(chǔ)方式的詳細(xì)介紹
本篇文章是對(duì)PHP中的session存儲(chǔ)方式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php 快速判斷一個(gè)數(shù)字屬于什么范圍的實(shí)現(xiàn)方法
這篇文章主要介紹了php 快速判斷一個(gè)數(shù)字屬于什么范圍的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-07-07PHP中利用substr_replace將指定兩位置之間的字符替換為*號(hào)
PHP的substr_replace將指定兩位置之間的字符替換為*號(hào)的代碼,需要的朋友可以參考下。2011-01-01PHP中new static()與new self()的區(qū)別異同分析
這篇文章主要介紹了PHP中new static()與new self()的區(qū)別異同分析,是很實(shí)用的技巧,需要的朋友可以參考下2014-08-08php使用Cookie實(shí)現(xiàn)和用戶會(huì)話的方法
這篇文章主要介紹了php使用Cookie實(shí)現(xiàn)和用戶會(huì)話的方法,分析了Cookie的原理、設(shè)置與使用技巧,需要的朋友可以參考下2015-01-01