简体   繁体   English

FreeBSD上PHP的MSSQL连接

[英]MSSQL connection from PHP on FreeBSD

As an extension of my question: Remote connect to SQL Server Standard Edition from PHP/FreeBSD 作为我的问题的扩展: 从PHP / FreeBSD远程连接到SQL Server标准版

I'm trying to get our FreeBSD/Apache/PHP server to be able to query our WinServer2003/SQL-Server-2000 box. 我正在尝试让我们的FreeBSD / Apache / PHP服务器能够查询我们的WinServer2003 / SQL-Server-2000盒子。 Please note that this is not for a public system, only for a internal reporting function - so performance is not the key at this point. 请注意,这不适用于公共系统,仅适用于内部报告功能 - 因此性能不是此时的关键。 The initial question helped me prepare the MSSQL server for connection, and is now getting some kind of response from it. 最初的问题帮助我准备MSSQL服务器进行连接,现在正在从中获得某种响应。 However I haven't got a successful connection to it. 但是我没有成功连接它。

I have tried PDO_dblib, mssql (FreeTDS) - haven't tried ODBC. 我试过PDO_dblib,mssql(FreeTDS) - 还没试过ODBC。 I would prefer if I could get PDO to work, so that's what I'm aiming at here - and it's also the most succesfull of the two I tried. 我更愿意,如果我能让PDO工作,那就是我的目标 - 这也是我试过的两个中最成功的。

My PHP script that utilizes PDO:dblib 我的PHP脚本使用PDO:dblib

<?php 

ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);

$servername = '192.168.1.51';
$port = '1433';
$serverdsn = $servername.':'.$port;
$username = 'webserver';
$password = '123456';
$dbname = 'oneServer_staging';

$sqlstatement = 'SELECT * FROM ordersp';

try 
        {
            $pdo = new PDO ("dblib:host=".$serverdsn.";dbname=".$dbname,$username,$password);
        } 
        catch (PDOException $e)
        {
            echo "Failed to get DB handle: " . $e->getMessage() . "\n";
            exit;
        }
exit; 

The result of this PDO:dblib script when running it in browser: 此PDO的结果:dblib脚本在浏览器中运行时:

Failed to get DB handle: SQLSTATE[28000] Login incorrect. (severity 9)

And ind the MSSQL server's application log I find this: 在MSSQL服务器的应用程序日志中,我发现:

在此输入图像描述

EDIT after comment about FreeTDS 关于FreeTDS的评论后编辑

My PHP script that utilizes mssql_connect() / FreeTDS 我的PHP脚本使用mssql_connect()/ FreeTDS

<?php 

ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);

$username = 'webserver';
$password = '123456';
$dbname = 'oneServer_staging';

$sqlstatement = 'SELECT * FROM ordersp';

$link = mssql_connect('MYMSDN', $username, $password);

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}

The result of this mssql/FreeTDS script when running it in browser: 在浏览器中运行时,此mssql / FreeTDS脚本的结果:

Warning: mssql_connect(): Unable to connect to server: MYMSDN in
/home/www/[..]/httpdocs/public/default/philip/oneserver-db-test.php
on line 17 Something went wrong while connecting to MSSQL

/usr/local/etc/freetds.conf /usr/local/etc/freetds.conf

[global]
        # TDS protocol version
        tds version = 4.2

        initial block size = 512

        # uses some fixes required for some bugged MSSQL 7.0 server tha
        # return invalid data to big endian clients
        # NOTE TDS version 7.0 or 8.0 should be used instead
;       swap broken dates = no
;       swap broken money = no

        # 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.
        # (Microsoft servers sometimes pretend TEXT columns are
        # 4 GB wide!)   If you have this problem, try setting
        # 'text size' to a more reasonable limit
        text size = 64512

# A typical Microsoft SQL Server 2000 configuration
[MYMSDN]
        host = 192.168.1.51
        port = 1433
        tds version = 8.0
        client charset = UTF-8

The file /tmp/freetds.log shows nothing when executing the script, however if i use # tsql -C command, it's updated. 文件/tmp/freetds.log在执行脚本时没有显示任何内容,但是如果我使用# tsql -C命令,它会更新。

Does the following lines of code work? 以下代码行是否有效?

ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);

$username = 'webserver';
$password = '123456';
$dbname = 'oneServer_staging';
$servername = '192.168.1.51';

if (!$link = mssql_connect($servername, $username, $password)) {
   exit('Error: Could not make a database connection using ' . $username . '@' . $servername);
}

if (!mssql_select_db($dbname, $link)) {
   exit('Error: Could not connect to database ' . $dbname);
}
?>

Do you need this line in your freetds.conf file? 你在freetds.conf文件中需要这一行吗? Are you sure both sides are using UTF-8? 你确定双方都在使用UTF-8吗?

client charset = UTF-8

Here's the code I use to connect PHP to MSSQL from Ubuntu machines to Windows SQL Server, I don't know if it will help you or not but this code is up and running successfully right now so I know it works in our environment... 这是我用来将PHP连接到MSSQL从Ubuntu机器到Windows SQL Server的代码,我不知道它是否会对你有所帮助,但是这段代码现在已经成功运行,所以我知道它在我们的环境中有效。 。

PHP: PHP:

<?php
try{
   $con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password);
}catch(PDOException $e){
   echo 'Failed to connect to database: ' . $e->getMessage() . "\n";
   exit;
}
?>

/etc/odbc.ini /etc/odbc.ini

# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssqldb]
Description             = MSSQL Server
Driver                  = freetds
Database                = MyDB
ServerName              = mssqldb
TDS_Version             = 8.0

/etc/odbcinst.ini /etc/odbcinst.ini

# Define where to find the driver for the Free TDS connections.
[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup           = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount      = 1

/etc/freetds/freetds.conf /etc/freetds/freetds.conf

[global]
        # 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

# Define a connection to the MSSQL server.
[mssqldb]
        host = mssqldb
        port = 1433
        tds version = 8.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM