Webbly Brown
Webbly Brown

Reputation: 1002

Connect PHP on Linux server to MS SQL on remote server running Windows

Im having real trouble getting this to work. I have got the mssql-connect drivers set up and working on the Linux server, it now understands that function and does not return any errors.

I have had our server management team configure the windows server to allow the connection from the linux server. They have tested this and confirm it is all working, however I just cant seem to connect to it with PHP.

I have tried various connection strings, but it won't connect, here is an example

<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server = '214.133.182.71,1443';

// Connect to MSSQL
$link = mssql_connect($server, '****', '******');

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

I always get: Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 214.133.182.71,1443 in /home/v3rec/public_html/test.php on line 10 Something went wrong while connecting to MSSQL

The username and password of that for the MS SQL database. Ive tried it with and without the port name. I am running SQL 2012, im not sure what the instance name would be? Am I missing something? Why is PHP unable to connect?

Upvotes: 1

Views: 2084

Answers (1)

Erik Flitman
Erik Flitman

Reputation: 163

May I suggest you use PDO and do this:

$dbhost  = "myhost";
$dbport  = 10060;
$dbname  = "tempdb";
$dbuser  = "dbuser";
$dbpass  = "password";
try {
    $dbh = new PDO ("dlib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
}

Upvotes: 3

Related Questions