PHP 連接 MSSQL

    系統環境:

    • CentOS 5.5
    • PHP 5.1.6-27 (PHP module: mssql.so)
    • MS SQL Server 2008
    • freetds-0.64-11.el5.centos
       

    How to Configure Linux ODBC Connections for MS SQL (with Asterisk):見附檔區

    步驟開始-mssql.so

    相依性套件安裝
    yum groupinstall "Development Tools"
    yum install php-devel freetds-devel
    yum install bzip2-devel db4-devel expat-devel gmp-devel aspell-devel httpd-devel libjpeg-devel pam-devel sqlite-devel pcre-devel libc-client-devel cyrus-sasl-devel openldap-devel  postgresql-devel  net-snmp-devel libxslt-devel gd-devel
    
    重編 SRPM 套件

    // 下載 SRPM:http://ftp.isu.edu.tw/pub/Linux/CentOS/5.5/os/SRPMS/

    cd /usr/src/redhat/SRPMS
    wget http://ftp.isu.edu.tw/pub/Linux/CentOS/5.5/os/SRPMS/php-5.1.6-27.el5.src.rpm 
    rpm -ivh php-5.1.6-27.el5.src.rpm
    vi ../SPECS/php.spec
    

    php.spec:
    在最後一個 %package...%description 加上

    %package mssql
    Group: Development/Languages
    Requires: php = %{version}-%{release}, php-pdo
    Summary: A module for PHP applications that use the MSSQL database.
    provides: php_database
    BuildRequires: freetds-devel
    
    %description mssql
    The MSSQL package contains a dynamic shared object that will add support for accessing MSSQL databases to php.

    找到這幾行,在 CFLAGS 這行刪除 -Wno-pointer-sign 。

    # Regenerate configure scripts (patches change config.m4's)
    ./buildconf --force
    
    CFLAGS="$RPM_OPT_FLAGS -fno-strict-aliasing"
    CPPFLAGS="-DLDAP_DEPRECATED=1"
    export CFLAGS CPPFLAGS
    
    # Install extension modules in %{_libdir}/php/modules.
    EXTENSION_DIR=%{_libdir}/php/modules; export EXTENSION_DIR
    

    重新編譯套件
    Notes: 如果有顯示缺少的套件,請先完成所有套件安裝。

    rpmbuild -bb ../SPECS/php.spec
    
    安裝 mssql.so 模組
    cd ../BUILD/php-5.1.6/ext/mssql/
    phpize
    ./configure --with-mssql
    make
    make install
    設定 PHP
    vi /etc/php.d/mssql.ini 
    

    mssql.ini:

    ; Enable mssql extension module
    extension=mssql.so
    

    重啟 httpd 服務

    檢查 phpinfo

    php_mssql.png

    測試連接

    修改 /etc/freetds.conf,新增

    [MSSQL_2K8]
       host = <mssql.server.ip.address>
       port = 1433
       client charset = UTF-8
       tds version = 8.0
       text size = 20971520
    

    demo.php:

    <?php
    $msconnect = mssql_connect("MSSQL_2K8","dbuser","dbpass");
    $msdb = mssql_select_db("ERP",$msconnect);
    $msquery = "SELECT * from DIM_FSA01_IVS where CUST_NO='MA01'";
    $msresults = mssql_query($msquery);
    while ($row = mssql_fetch_array($msresults)) {
           echo "<li>" . $row['CUST_NO'] . " " . $row['PKG_TYPE'] . " " . $row['IV_TIME'] .  "</li>\n";
    }
    ?>
    

    Notes:

    * MSSQL_2K8,必須與 freetds 設定相同,後面是登入DB 的帳號及密碼。
    * ERP,要連結的 DB Name。

    F.A.Q

    如何快速在 Linux 系統上測試與 MSSQL Server 的資料庫連接?
    Ans: 指另如下,

    tsql -S <mssql.server.ip.address> -U <dbuser> -P <dbpass>
    
    標籤 (Edit tags)
    • No tags

    文件 2

    文件大小日期附件上傳者 
     How to Configure Linux ODBC Connections for MS SQL.png
    How to Configure Linux ODBC Connections for MS SQL
    1267.41 KB09:48, 21 Oct 2012alang動作
     php_mssql.png
    PHP MSSQL Module
    20.36 KB11:07, 18 Mar 2011alang動作
    您必須 登入 才能發佈評論。
    Powered by MindTouch Core