5

I'm trying to get wamp php to communicate with an MsSQL DB but I can't seem to make it work. The current error message I get on the browser (Chrome) is:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: (...) Couldn't connect to SQL Server
  1. ntwdblib.dll is the correct version (2000.80.194.0)

  2. I can't use php_sqlsrv (the deployment server is linux only, sqlsrv is windows only), although from using sqlsrv I can conclude that the access info is correct (server, user, password)

  3. I've downgraded back to wampserver 2.1e & php 5.3.1 (some people got it working like this)

  4. there are no errors on the apache_error.log when I restart the wamp server

  5. installing freetds on my machine hangs apache

  6. remote machine successfully uses php_mssql.dll (linux server)

The code I'm using:

<?php
$dbhandle = mssql_connect($ip,$user,$pass) or die("Couldn't connect to SQL Server on $ip"); 
$selected = mssql_select_db($dbname,$dbhandle) or die("Couldn't open database at SQL server: $ip");
?>

I am now going to try a linux virtual machine, but surely even if that works it will be a pain (slow and feature-less).

My system: Windows 7, Wampserver 2.1e, PHP 5.3.1

What else can I try? :(

Tjoene
  • 320
  • 9
  • 18
Pedro Araujo Jorge
  • 642
  • 2
  • 9
  • 19
  • Can you show the code you have tried? – Tjoene Feb 28 '13 at 17:07
  • Sure, here is my code: $dbhandle = mssql_connect($ip,$user,$pass) or die("Couldn't connect to SQL Server on $ip"); $selected = mssql_select_db($dbname,$dbhandle) or die("Couldn't open database at SQL server: $ip"); – Pedro Araujo Jorge Mar 01 '13 at 08:43
  • Still stuck on this. Right now I'm using a function that checks if the website is online. If it is it calls mssql (which works on the linux server), if it ain't it calls fake tables on mysql, which have the same structure as the mssql tables. This works but it is far from ideal... :P – Pedro Araujo Jorge Mar 06 '13 at 09:54

4 Answers4

2

Assuming you've tried to get the driver/extension installed from here: http://www.microsoft.com/en-gb/download/details.aspx?id=20098 and you've changed your php.ini in your *AMP stack's PHP folder..; and put the mssql extension in the php/ext folder...

after several hours of looking, this guide successfully got me connected to a MS SQL database. http://webcheatsheet.com/php/connect_mssql_database.php

edit: furthermore my connection class takes DB Uname PW and serverIP as constructor parameters, and the constructor calls function setCon(), so when the object is created the connection is too. then I can call queries on the connection object it in an OOP style

    function setCon(){
    $conn = mssql_connect($this->server, $this->UName, $this->pw);
    if ($conn){
        echo "Connection established @ $this->server.$this->DB (Connected to MS SQL DB)\n";
    }
    else{
        echo "Connection could not be established.";
        die( print_r( sqlsrv_errors(), true));
    }

and then later this is needed to access the connection created

function getCon(){
    return mssql_connect($this->server, $this->UName, $this->pw);
}
J-Dizzle
  • 3,176
  • 6
  • 31
  • 49
0

Did you install MsSQL ? Because WAMP's M is MySQL not MSSQL.

Cyril ALFARO
  • 1,616
  • 17
  • 35
0

Unable to connect to server: (...) Couldn't connect to SQL Server"

Did you install any driver module within PHP for MS-SQL ??

Besides a VM isn't necessarily slow and featureless - at least it works :)

MDB2 or PDO might be most suitable for abstracting the database I/O.

Q: Can you open a terminal to MS-SQL, e.g. Telnet??

(This would at least sort out any kind of connectivity issues).

You might need this module here: PECL odbtp >= 1.1.1 (open database transport protocol)

extension=php_mssql.dll

^ also, checkout phpinfo() once if this extension is even loaded.

http://www.php.net/manual/en/mssql.installation.php

Well - installing mySQL on Windows wouldn't be any problem (in case that would make sense).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Thanks for your reply. I installed the php_mssql extension and there are no errors reported by apache. I also know there are no connectivity issues because if I use the sqlsrv extension (with the same settings) everything works fine. – Pedro Araujo Jorge Mar 01 '13 at 08:57
0

Actualy by default wampserver doesn't come with php_mssql.dll the extension is made on top of php_pdo_mssql or php_dblib. Tested on x64 wampserver 2 PHP 5.3.13

  • Using php_pdo_mssql

    1. Install wampserver
    2. Activate php_pdo_mssql
    3. Activate php_mssql
    4. copy ntwdblib.dll to bin/php/php{version}/ext/ and bin/apache/{version}/bin
    5. restart all services
  • Using php_dblib

    1. instead of php_pdo_mssql activate php_dblib.

Warning do not activate both php_pdo_mssql and php_dblib.

On different versions of php wampserver works different on 5.3.1 first solution worked for me but on 5.3.13 second.