简体   繁体   中英

Apache will not connect to SQL Server with PHP

I'm trying to open a connection to a SQL Server 2008 on a windows computer from an apache linux server via php. I've opened up the appropriate port on the firewall, but I am getting the vaguest error

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xx.xxx,xxxx

with:

$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
$myUser = "un";
$myPass = "pw";
$myDB = "db"; 

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die( mssql_get_last_message()); 

Is there some way to get a more specific error message? Or some way that I can test to see if the two computers are communicating at all so I can try to begin to localize the problem?

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...

As you can see, I use PDO rather than the mssql_* functions. On Ubuntu I needed to install the php5-sybase package to get the dblib driver.

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

# 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

# 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

[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

I ran into the same problem today. This works for me:

Instead of this

$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon

try this:

$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file

In your case, I'd rename the entry and use the fully qualified hostname to the entry in the config file just for clarity

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

The first argument in the call to mssql_connect() must correspond to an [entry] in the freetds.conf file. So your call becomes

$myServer = "mssqldbAlias"; 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die( mssql_get_last_message());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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