标签 BT 下的文章

在 CentOS 7 宝塔面板软件环境中安装unixODBC和FreeTDS - 使用php的 odbc_connect 函数 连接 SQL Server 2000

需求:在CentOS7 的宝塔面板软件环境中,已经安装有 nginx + php7.3。需要通过如 $connect = odbc_connect("DRIVER=FreeTDS; Server=192.168.97.65; Port=1433; TDS_Version=7.1; Database=master", 'sa', 'aqtest'); 的php文件连接到 SQL Server 2000

尝试:

  1. 下载 unixODBC-2.3.9 源码文件。再配置、编译和安装。如本例,是将unixODBC安装到/usr/local/unixODBC的路径,所以在配置时,是 ./configure --prefix=/usr/local/unixODBC
  2. 下载 freetds-1.3.4 源码文件。再配置、编译和安装。如本例,是将FreeTDS安装到/usr/local/freetds的路径,在配置时,除了要加上自定义的安装路径,还要加上unixODBC的参数,如 ./configure --with-tdsver=7.4 --prefix=/usr/local/freetds --enable-msdblib --with-unixodbc=/usr/local/unixODBC
  3. 如果宝塔环境中,安装的php版本是7.3.33的,请下载 php-7.3.33 源码文件。解压后,在原宝塔的php配置参数基础上加 '--with-unixODBC=/usr/local/unixODBC' ,如本例,再编译和安装

    [root@localhost php-7.3.31]# './configure'  '--prefix=/www/server/php/73' '--with-config-file-path=/www/server/php/73/etc' '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--enable-mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-iconv-dir' '--with-freetype-dir=/usr/local/freetype' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl=/usr/local/curl' '--enable-mbregex' '--enable-mbstring' '--enable-intl' '--enable-ftp' '--with-gd' '--with-openssl=/usr/local/openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-soap' '--with-gettext' '--disable-fileinfo' '--enable-opcache' '--with-sodium' '--with-webp' '--with-unixODBC=/usr/local/unixODBC'
  4. 如果顺利,那么新版本的文件会覆盖原宝塔安装的php文件
  5. 新版本的phpinfo信息输出中,应该有unixODBC的信息详情,如:

    ODBC Support => enabled
    ODBC library => unixODBC
    ODBCVER => 0x0380
    ODBC_INCLUDE => -I/usr/local/unixODBC/include
    ODBC_LFLAGS => -L/usr/local/unixODBC/lib
    ODBC_LIBS => -lodbc
  6. php命令行可返回结果,如

    #php -r "var_dump(odbc_connect('DRIVER={freetds};SERVER=192.168.97.115;PORT=1433;DATABASE=master','sa','aqtest'));"
    resource(4) of type (odbc link)
  7. 在web里测试,php使用 odbc_connect 函数 也正常,如:

    [root@localhost local.anqun.org]# curl http://192.168.97.65/5.php
    master 1
    tempdb 2
    model 3
    msdb 4
  8. 测试文件5.php的内容如下(SQLServer 2008 Express):

    <?php
    
    # connect to 192.168.97.115 with a user "sa" and password "aqtest"
    $connect = odbc_connect("DRIVER=freetds;SERVER=192.168.97.115;PORT=1433 DATABASE=master",'sa','aqtest');
    
    # query the users table for all fields
    $query = "select * from sysdatabases";
    
    # perform the query
    $result = odbc_exec($connect, $query);
    
    # fetch the data from the database
    while(odbc_fetch_row($result)) {
    $field1 = odbc_result($result, 1);
    $field2 = odbc_result($result, 2);
    print("$field1 $field2\n");
    }
    
    # close the connection
    odbc_close($connect);
    
    ?>

dn_bt-php-unixodbc-freetds-sqlserver-1.png

参考: