【问题标题】:connect mssql using php on Centos在 Centos 上使用 php 连接 mssql
【发布时间】:2013-06-14 05:27:35
【问题描述】:

我已经安装了 freetds,我想通过 php 连接到 mssql 服务器。我正在使用 centos。

root@server [~/php]# tsql -C
Compile-time settings (established with the "configure" script)
                            Version: freetds v0.91
             freetds.conf directory: /usr/local/etc
     MS db-lib source compatibility: no
        Sybase binary compatibility: no
                      Thread safety: yes
                      iconv library: yes
                        TDS version: 5.0
                              iODBC: no
                           unixodbc: yes
              SSPI "trusted" logins: no
                           Kerberos: no

我用来连接 mssql 服务器的 php 代码。 (它位于子目录中)我能够通过本地 mssql 服务器 2008 连接到远程 mssql 服务器。

<?php
// Older FreeTDS installations need the FREETDSCONF Environment variable
putenv('FREETDSCONF=../usr/local/etc/freetds.conf');
// Current release of FreeTDS uses the FREETDS environment variable. So we set both to be sure
putenv('FREETDS=../usr/local/etc/freetds.conf');
$mssql = mssql_connect('116.214.26.236' , 'dbname', 'password');
//mssql_select_db('myDb',$mssql);


if (!$mssql) {
    die('Something went wrong while connecting to MSSQL');
}else echo 'Success';

?>

我的 TDS 配置文件

#   $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

# A typical Sybase server
[egServer50]
    host = symachine.domain.com
    port = 5000
    tds version = 5.0

# A typical Microsoft server
[egServer70]
    host = 116.214.26.236
    port = 1433
    tds version = 7.0

我得到的错误

警告:mssql_connect() [function.mssql-connect]:无法连接到服务器:第 6 行 /home/butter/public_html/investonline/mssql.php 中的 116.214.26.236 连接到 MSSQL 时出现问题

请指导我哪里出错了。

我在谷歌上搜索过一些博客说下面的行值必须是yes, MS db-lib 源兼容性:否

我的 php.ini 配置文件

mssql
MSSQL Support   enabled
Active Persistent Links 0
Active Links    0
Library version FreeTDS

Directive   Local Value Master Value
mssql.allow_persistent  On  On
mssql.batchsize 0   0
mssql.charset   no value    no value
mssql.compatability_mode    Off Off
mssql.connect_timeout   5   5
mssql.datetimeconvert   On  On
mssql.max_links Unlimited   Unlimited
mssql.max_persistent    Unlimited   Unlimited
mssql.max_procs Unlimited   Unlimited
mssql.min_error_severity    10  10
mssql.min_message_severity  10  10
mssql.secure_connection On  On
mssql.textlimit Server default  Server default
mssql.textsize  Server default  Server default
mssql.timeout   60  60

还请告诉我应该如何获取日志,以便我知道在哪一步出现错误。

还有为什么我无法连接到 mssql 服务器。

【问题讨论】:

    标签: php sql-server freetds


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      你可以使用PDO php库来连接sql server,你可以这样使用 $output = new PDO ('mssql:host=SERVERHOST;dbname=DBNAME','DBUSERNAME','DBPASSWORD');

      【讨论】:

      • $dsn = 'mssql:host=116.214.26.236;dbname=dbname'; $user = '用户名'; $password = '密码';尝试 { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo '连接失败:' 。 $e->getMessage(); }
      • 您必须启用 PDO 库,然后才能连接。您可以从您的 php.ini 文件中执行此操作。
      • 我应该取消注释哪一行?
      • php_pdo_mssql.dll 应该被取消注释。如果您没有此dll,则必须下载并放入ext目录。
      • 下载并添加文件到 ext dir 将行 extension = "php_pdo_mssql.dll" 添加到 php ini in user/lib/ dir 它仍然说 Connection failed: could not find driver
      猜你喜欢
      • 2018-02-07
      • 2012-12-06
      • 1970-01-01
      • 2016-08-17
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      相关资源
      最近更新 更多