Connecting to MS SQL Server 2008 Express from PHP 5

Soldato
Joined
1 Feb 2006
Posts
3,793
Hi,
I am trying to get PHP 5 to connect to MS SQL Server 2008 express. PHP is installed on a VirtualBox VM that’s running Ubuntu 10.04 LTS. The host OS is Win7 HP x64 and I have setup a network bridge so I can access the PHP website I am working on from the host. The webserver is Apache2.
I have searched the net and tried several methods but cannot get a connection?
 
I've used the following on a PHP app running on XAMPP on Windows, connecting to a MS SQL server.

PHP:
<?php

$ms_dbhost = 'server';
$ms_dbuser = 'username';
$ms_dbpass = 'password';
$ms_dbname = 'dbname';

$ms_conn = mssql_connect($ms_dbhost, $ms_dbuser, $ms_dbpass) or die		('Error connecting to MSSQL');

mssql_select_db($ms_dbname);

?>

You then do with that exactly what you'd do with a MySQL database.
 
Hi Confused,
I have tried code like that:
Code:
<?php
$myServer = "192.168.56.101\SQLEXPRESS";
$myUser = "ted";
$myPass = "123";
$myDB = "publico";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)  or die("Couldn't connect to SQL Server on: " . mssql_get_last_message());
//close the connection
mssql_close($dbhandle);
?>

For the $myServer var I have tried using:
IP address, IP + port, instance name but none of them connect?
From windows (the host pc) I use the instance name Flint_7-PC\SQLEXPRESS to connect with a windows app I created which works.
I think it could be the firewall or a network setting but don’t know much about that so I am stuck :-( What value do you use for $ms_dbhost when you connect?
 
I'm not using a named instance, so it was just 'servername'.

However, from a quick search, try using a \\ in the name, rather than \, so:

$myServer = "192.168.56.101\\SQLEXPRESS";

or

$myServer = "Flint_7-PC\\SQLEXPRESS";
 
Thanks for posting Confused.
Have just tried adding the extra \ but still not connecting. I also turned the firewall off but did nothing?.

I just tried using the db app that works on my pc on a laptop over wifi and that can not connect, so it must be a network setting that is stopping it. I used a network monitor to see if data was sent when i try and connect and it is but sql server dose not send any back?
 
Last edited:
Back
Top Bottom