分享java帶來的快樂

          我喜歡java新東西

          Linux Php連接SQLServer數據庫(freetds)

          由于工作原因我們需要通過PHP訪問我們以前的Sql Server 2005數據,所以就有了這篇文章的誕生.廢話就少說了,做程序設計的最不喜歡兜圈子了.用簡介步驟說明問題,往下看.

            系統: Linux

            數據庫: Sql Server 2005

            1.下載FreeTDS

            官方網站:http://www.freetds.org

            2.安裝FreeTDS

            # tar zxvf freetds-current.tgz(解壓)

            # ./configure --prefix=/usr/local/freetds --with-tdsver=7.2 --enable-msdblib

            # make

            # make install

            其他可選 根據自己情況

            --enable-dbmfix --with-gnu-ld --enable-shared --enable-static

            安裝freetds到目錄/usr/local/freetds:--prefix=/usr/local/freetds 如果不帶這個默認好像也是這目錄

            對應數據庫版本--我的是Microsoft SQL Server 2005 所以我帶的是 --with-tdsver=7.2

            4.2 Sybase SQL Server < 10 and Microsoft SQL Server 6.5

            5.0 Sybase SQL Server >= 10

            7.0 Microsoft SQL Server 7.0

            7.1 Microsoft SQL Server 2000

            7.2 Microsoft SQL Server 2005

            3.編輯/usr/local/freetds/etc/freetds.conf

            # $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $

            #

            # This file is installed by FreeTDS if no file by the same

            # name is found in the installation directory.

            #

            # For information about the layout of this file and its settings,

            # see the freetds.conf manpage "man freetds.conf".

            # Global settings are overridden by those in a database

            # server specific section

            [global]

            # TDS protocol version

            ; tds version = 4.2

            # Whether to write a TDSDUMP file for diagnostic purposes

            # (setting this to /tmp is insecure on a multi-user system)

            ; dump file = /tmp/freetds.log

            ; debug flags = 0xffff

            # Command and connection timeouts

            ; timeout = 10

            ; connect timeout = 10

            # If you get out-of-memory errors, it may mean that your client

            # is trying to allocate a huge buffer for a TEXT field.

            # Try setting "text size" to a more reasonable limit

            text size = 64512

            #解決中文亂碼問題

            client charset=utf8

            # A typical Sybase server

            #[egServer50]

            # host = symachine.domain.com

            # port = 5000

            # tds version = 5.0

            # A typical Microsoft server

            #[egServer70]

            # host = ntmachine.domain.com

            # port = 1433

            # tds version = 7.0

            #這個名字程序和命令行用得上,叫什么自己定

            [Server2005]

            host = 192.168.3.100 #我的SQL Server2005 IP,根據自己改

            port = 1433

            tds version = 7.2

            4.測試連接:

            [root@test bin]# ./tsql -S Server2005 -p 1433 -U java -P java -D PublicDB

            locale is "zh_CN"

            locale charset is "GB2312"

            Default database being set to PublicDB

            1>

            出現這個表示連接成功! 退出:quit 和 exit 都行.

            參數說明

            -S 配置的服務名

            -H 主機名

            -p 端口

            -U username

            -P password

            -D database

            5.測試查詢:

            # ./tsql -S Server2005 -p 1433 -U java -P java -D PublicDB

            1> select USER_ID,TRUE_NAME from USER_INFO

            2> go

            可以顯示中文沒問題!

            6.讓PHP支持mssql(freeTDS)

            重新編譯PHP 這些參數根據自己情況來定,下面是我們需要的

            但是必須帶--with-mssql=/usr/local/freetds

            ./configure --prefix=/usr/local/PHP --with-MySQL=/usr/local/MySQL --with-apxs2=/usr/local/apache/bin/apxs --with-gd=/usr/local/gd --with-jpeg-dir=/usr/local/libjpeg --with-png-dir=/usr/local/libpng --with-zlib-dir=/usr/local/zlib --with-libxml-dir=/usr/local/libxml2 --with-iconv=/usr/local/libiconv --with-freetype-dir=/usr/local/freetype --with-pdo-MySQL=/usr/local/PHPbak/lib/PHP/extensions/no-debug-non-zts-20060613/pdo_MySQL.so --enable-sockets --with-curl --with-pear --with-mssql=/usr/local/freetds

            如果編譯報錯請執行:

            # touch /usr/local/freetds/include/tds.h

            # touch /usr/local/freetds/lib/libtds.a

            7.PHP測試程序

            

            /**

            * MOIT

            *

            * @author 明白(admin126com@126.com) 日 期: Wed Nov 18 05:00:07 GMT 2009

            * @copyright Copyright (c) 2009

            * @desc 測試

            */

            $msconnect=mssql_connect("Server2005","java","java");

            $msdb=mssql_select_db("PublicDB",$msconnect);

            $msquery = "select TRUE_NAME,USER_ID,USER_NAME,PASSWORD from USER_INFO";

            $msresults= mssql_query($msquery);

            while ($row = mssql_fetch_array($msresults)) {

            echo $row["USER_ID"] . " ".$row["TRUE_NAME"]. " " . $row["USER_NAME"] . " " . $row["PASSWORD"] . "
          ";

            }

            ?>

            8.安裝完畢,祝您成功!

          posted on 2011-02-28 22:03 強強 閱讀(1001) 評論(0)  編輯  收藏 所屬分類: linux 操作系統配置 、php

          主站蜘蛛池模板: 泰宁县| 洪湖市| 阿巴嘎旗| 延长县| 广元市| 文成县| 乐平市| 宝山区| 互助| 江源县| 交口县| 扎赉特旗| 康保县| 木里| 富民县| 衡南县| 漳州市| 会宁县| 田阳县| 五台县| 马龙县| 库伦旗| 甘肃省| 张北县| 会同县| 马关县| 大埔区| 宁晋县| 平潭县| 汕头市| 纳雍县| 安康市| 汉沽区| 阿拉善左旗| 濮阳市| 湖北省| 肥城市| 芜湖市| 甘泉县| 聊城市| 句容市|