简体   繁体   中英

php apache (xampp) odbc connection issue

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

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, eg

// 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 )

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