關(guān)于php連接mssql:pdo odbc sql server
更新時間:2011年07月20日 00:39:33 作者:
研究了很久,終于發(fā)現(xiàn):最新的php 5.3.6中php_mssql.dll,php_pdo_mssql.dll都已經(jīng)不見了。
只有一個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ù)庫的問題
今天配置了新服務(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ù)庫主機(jī)變量設(shè)置是否正確!!!
而主機(jī)的變量設(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ù)器后,一切正常。
補(bǔ)充:如果數(shù)據(jù)庫名的開頭是數(shù)字時也會提示無法打開,這時要做的很簡單,把數(shù)據(jù)庫的名字用中括號 [ ]
括起來就搞定了,如 123bbs 改寫成 [123bbs]就沒有問題了,另外如果你的數(shù)據(jù)庫名字與sql server中的保留字沖突的話也會出現(xiàn)這種情況,用中括號的方法一樣可以解決。
最終,PHP無法正確連接sql server 2000的問題終于解決了,雖然耗費大半天的時間,但收獲還是很大的,現(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ù)庫的問題
今天配置了新服務(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ù)庫主機(jī)變量設(shè)置是否正確!!!
而主機(jī)的變量設(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ù)器后,一切正常。
補(bǔ)充:如果數(shù)據(jù)庫名的開頭是數(shù)字時也會提示無法打開,這時要做的很簡單,把數(shù)據(jù)庫的名字用中括號 [ ]
括起來就搞定了,如 123bbs 改寫成 [123bbs]就沒有問題了,另外如果你的數(shù)據(jù)庫名字與sql server中的保留字沖突的話也會出現(xiàn)這種情況,用中括號的方法一樣可以解決。
最終,PHP無法正確連接sql server 2000的問題終于解決了,雖然耗費大半天的時間,但收獲還是很大的,現(xiàn)在把它貼出來,也讓遇到同樣問題的兄弟們少走一些彎路。
您可能感興趣的文章:
- PHP5中使用PDO連接數(shù)據(jù)庫的方法
- php中mysql連接方式PDO使用詳解
- php使用pdo連接sqlserver示例分享
- php使用pdo連接mssql server數(shù)據(jù)庫實例
- php中數(shù)據(jù)庫連接方式pdo和mysqli對比分析
- php使用pdo連接sqlite3的配置示例
- PHP使用PDO連接ACCESS數(shù)據(jù)庫
- php基于PDO連接MSSQL示例DEMO
- php PDO判斷連接是否可用的實現(xiàn)方法
- PHP實現(xiàn)基于PDO擴(kuò)展連接PostgreSQL對象關(guān)系數(shù)據(jù)庫示例
- PHP使用PDO 連接與連接管理操作實例分析
相關(guān)文章
php Notice: Undefined index 錯誤提示解決方法
字面意思就是未定義的索引,一般情況下是因為程序開發(fā)作者判斷不嚴(yán)謹(jǐn)導(dǎo)致。一般不會影響程序的運行,具體的解決方法可以參考下。2010-08-08
php中g(shù)et_cfg_var()和ini_get()的用法及區(qū)別
這篇文章主要介紹了php中g(shù)et_cfg_var()和ini_get()的用法及區(qū)別,實例分析了get_cfg_var()和ini_get()函數(shù)的區(qū)別與使用技巧,需要的朋友可以參考下2015-03-03
PHP使用PDO操作數(shù)據(jù)庫的亂碼問題解決方法
這篇文章主要介紹了PHP使用PDO操作數(shù)據(jù)庫的亂碼問題解決方法,實例分析了編碼設(shè)置及屬性設(shè)置解決亂碼問題的實現(xiàn)技巧,需要的朋友可以參考下2016-04-04

