ARCHIVED: Connect your MS SQL database to Webserve using ODBC

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.
Note:

Due to the evolving coronavirus situation, the retirement of Webserve was delayed until April 16 to allow classes that reference sites on Webserve to continue with minimal disruption.

All remaining Webserve sites were disabled beginning April 16, along with login access to webserve.iu.edu. If you have websites that still need to be moved to IU Sitehosting, you should migrate immediately.

To connect your MS SQL database to Webserve, you will need to use an ODBC connection.

If your database uses ADS account authentication, it will not work for database connections from Webserve. Instead, you will need to use a local MS SQL username/password. You should also use the full DNS host name of the MS SQL server, and not just its ADS network name.

Because Webserve is a shared environment, you will need to put your ODBC datasource definitions in a file in your account home directory:

/ip/username/.odbc.ini

The contents of ~/.odbc.ini should look like the following (with your database information substituted):

[<datasource_name>]

Driver = FreeTDS

Description = Microsoft SQL Server 2012 (or another version)

Server = <db_server_hostname>

Database = <database_name>

Port = 1433 (optional if using the standard port)

TDS_Version = 7.4

You can test the connection with the following code:

<?php

$user = '<db_username>';

$pass = '<db_password>';

$dsn = '<datasource_name>';

 
$dbc = odbc_connect ($dsn, $user, $pass);

 
if ($dbc) {

echo "Database Connection Successful";

odbc_close($dbc);

} else {

die("Connection could not be established");

}

?>

This is document atwx in the Knowledge Base.
Last modified on 2021-09-08 10:20:20.