0

When I run apache (via xampp) as a standalone server not as a service (on Windows Server 2008)

with the following connection code everything works fine (username and password removed )

$server = "WMS";
$link  = odbc_connect($server,'','');

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

If however I change apache to run as a service in Windows the connection breaks and I get the following error message

    Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] 
Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in 
C:\xampp\htdocs\Dev\well.php on line 30

     Something went wrong while connecting to MSSQL

1 Answer 1

1

Please read documentation: http://uk.php.net/manual/en/function.odbc-connect.php

$server = "WMS"; suggests that you have ODBC alias/data source configured with that name. Error message clearly says that data source with such name (WMS) is not found. On Windows 7/Vista/XP/Server you can configure them at "Start -> Administrative Tools -> Data Sources (ODBC)" -- path can be different on older OS. In any case -- look for "Microsoft ODBC Data Source Administrator".

Instead of using alias, I would recommend (the way I always connect) to use full DSN name, e.g.

// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$link = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);

In this case everything is part of the script and no external dependencies.

BTW -- instead of using ODBC Functions, I would recommend using PDO & driver specially for MS SQL Server: PDO_SQLSRV -- http://uk.php.net/manual/en/ref.pdo-sqlsrv.php (or Microsoft SQL Server Driver for PHP if you prefer old procedural style -- http://uk.php.net/manual/en/book.sqlsrv.php )

Sign up to request clarification or add additional context in comments.

6 Comments

I do have a DATA source (ODBC) setup with the name WMS, I think that's how its working now because if I change $server = "WMS"; to something else it doesn't connect (when apache is running not as a service) perhaps I need the driver info in the odbc_connect line I will try
also I am using SQL server 2012 does that line also apply to 2012? or should I be doign something different?
I'm using the line below to connect to SQL Server 2008 R2, as well as SQL Server 2005 (it all depends on SQL Server Native Client version you have - mine is from SQL Server 2008) -- as far as I know the connection string should be the same for 2012 version. 'Provider=SQLNCLI10;SERVER='.CFG_DB_SERVER.';UID='.CFG_DB_LOGIN.';PWD='.CFG_DB_PASSWORD.';DATABASE='.CFG_DB_DATABASE.';'
Small correction -- if using Native Client from 2012, the Provider should be SQLNCLI11 -- msdn.microsoft.com/en-us/library/ms131291.aspx
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008 $link = odbc_connect("Driver={SQL Server Native Client 11.0};Server=$server;Database=$database;",$user,$password); Works for me now, but I was tring to use windows authentication, and needed to setup SQL Server as the authentication method and setup a user in the login table in SQL Server 2012
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.