dousongxuan7507
2014-12-09 15:58
浏览 119
已采纳

如何配置wampserver使用PDO连接到mssql服务器?

I am trying to connect to a MSSQL DB server using PDO. I have downloaded some .dll files from Microsoft's web page and copied them to wampserver's wamp\bin\php\php5.5.12\ext folder. Then I edited php.ini which is located at wamp\bin\apache\apache2.4.9\bin folder.

I added the following lines:

extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
;extension=php_sqlsrv_55_nts.dll
;extension=php_pdo_sqlsrv_55_nts.dll

I have shutdown wampserver and run it again. I can see those four extensions on the menu PHP -> PHP Extensions, when I click on wampserver's tray icon.

Then I have tried to create a connection using the code below:

$conn = new PDO('sqlsrv:Server=1.2.3.4,1433;Database=database.mdf', 
                'user', 'password', 
                array(PDO::ATTR_EMULATE_PREPARES => false,
                      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
               );

When I call the page from browser I get

could not find driver

error.

First, How can I connect to mssql server with PDO? Second, Is there any point I missed about insatallation and/or configuration?

EDIT

Below, you can see my php.ini file's extensions section.

extension=php_bz2.dll
extension=php_curl.dll
extension=php_com_dotnet.dll
extension=php_fileinfo.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_gmp.dll
extension=php_intl.dll
extension=php_imap.dll
;extension=php_interbase.dll
extension=php_ldap.dll
extension=php_mbstring.dll
extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysql.dll
extension=php_mysqli.dll
;extension=php_oci8.dll      ; Use with Oracle 10gR2 Instant Client
;extension=php_oci8_11g.dll  ; Use with Oracle 11gR2 Instant Client
extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
extension=php_shmop.dll
; The MIBS data available in the PHP distribution must be installed. 
; See http://www.php.net/manual/en/snmp.installation.php 
;extension=php_snmp.dll
extension=php_soap.dll
extension=php_sockets.dll
extension=php_sqlite3.dll
;extension=php_sybase_ct.dll
;extension=php_tidy.dll
extension=php_xmlrpc.dll
extension=php_xsl.dll  
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll
;extension=php_sqlsrv_55_nts.dll
;extension=php_pdo_sqlsrv_55_nts.dll

EDIT 2:

Finally I have discovered that my dll files ara incompatible with my current wampserver64. I am looking for the correct files.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duan111112 2014-12-10 16:35
    已采纳

    I have uninstalled WampServer64 and installed WampServer32. Then everything is OK now.

    My connection object

    $conn = new PDO('sqlsrv:Server='.$host.';Database=' . $db, $user, $pass, 
                    array(
                          PDO::ATTR_EMULATE_PREPARES => false,
                          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                         )
                   );
    

    There is another point: I have incidently typed .mdf extension after database name which should not be. Because if I write it I get

    28000 - SQLSTATE[28000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'zkanoca_dbuser'.

    error.

    打赏 评论
  • dongzhenqi2015 2014-12-09 16:14

    You must enable:

    extension=php_pdo_mssql.dll
    

    in your php.ini.

    打赏 评论
  • douyue2313 2014-12-09 16:30

    I have a windows server, where I run PHP, I just checked here are the extensions I have enabled:

    extension=php_mssql.dll
    extension=php_pdo.dll
    extension=php_pdo_mssql.dll
    extension=php_pdo_odbc.dll #(might not need it)
    

    And your connection would look like this:

    $conn = new PDO('mssql:host=1.2.3.4;dbname=database', 'user', 'password');
    

    Hope this helps.

    打赏 评论

相关推荐 更多相似问题