如何使用PHP通过unixodbc和freetds连接MYSQL?

时间:2012-12-14 10:05:21

标签: php macos odbc osx-lion freetds

在Lion上安装了

As described in a previous questionfreetdsunixodbc并使用isql进行了测试,我尝试使用PHP连接到MSSQL:

$connection = odbc_connect('SMS_GTWY', 'username', 'password');

然而,它返回:

PHP Warning:  odbc_connect(): SQL error: [iODBC][Driver Manager]Specified driver could not be loaded, SQL state IM003 in SQLConnect in testodbc.php line 3

[iODBC][Driver Manager]Specified driver could not be loaded

我该如何解决?

P.S。执行上述PHP后输出$ cat /tmp/odbc_trace

** iODBC Trace file
** Trace started on Fri Dec 14 18:03:27 2012
** Driver Manager: 03.52.0607.1008


[000000.000472]
php             7FFF738F8960 EXIT  SQLConnect with return code -1 (SQL_ERROR)
        SQLHDBC           0x7fbec48a77d0
        SQLCHAR         * 0x10e2e8ea8
        SQLSMALLINT       -3 (SQL_NTS)
        SQLCHAR         * 0x10e2e7300
        SQLSMALLINT       -3 (SQL_NTS)
        SQLCHAR         * 0x10e1873fa
        SQLSMALLINT       -3 (SQL_NTS)

[000000.000540]
php             7FFF738F8960 ENTER SQLError
        SQLHENV           0x7fbec48a7560
        SQLHDBC           0x7fbec48a77d0
        SQLHSTMT          0x0 (SQL_NULL_HANDLE)
        SQLCHAR         * 0x7fff6d1adf42
        SQLINTEGER      * 0x7fff6d1adf3c
        SQLCHAR         * 0x7fff6d1adf48
        SQLINTEGER        511
        SQLSMALLINT     * 0x7fff6d1adf3a

[000000.000598]
php             7FFF738F8960 EXIT  SQLError with return code 0 (SQL_SUCCESS)
        SQLHENV           0x7fbec48a7560
        SQLHDBC           0x7fbec48a77d0
        SQLHSTMT          0x0 (SQL_NULL_HANDLE)
        SQLCHAR         * 0x7fff6d1adf42
                  | IM003                                    |
        SQLINTEGER      * 0x7fff6d1adf3c (0)
        SQLCHAR         * 0x7fff6d1adf48
                  | [iODBC][Driver Manager]Specified driver  |
                  | could not be loaded                      |
        SQLINTEGER        511
        SQLSMALLINT     * 0x7fff6d1adf3a (59)

[000000.000784]
php             7FFF738F8960 ENTER SQLFreeConnect
        SQLHDBC           0x7fbec48a77d0

[000000.000807]
php             7FFF738F8960 EXIT  SQLFreeConnect with return code 0 (SQL_SUCCESS)
        SQLHDBC           0x7fbec48a77d0

1 个答案:

答案 0 :(得分:1)

要解决此问题,请使用FREETDSCONFODBCINIODBCINSTINI设置环境:

<?php

  putenv("FREETDSCONF=/Users/ohho/.freetds.conf");
  putenv("ODBCINI=/Users/ohho/.odbc.ini");
  putenv("ODBCINSTINI=/Users/ohho/.odbcinst.ini");

  $connection = odbc_connect('SMS_GTWY', 'sms_gtwy', 'sms_gtwy');

?>