MSSQL with Apache and PHP on Linux

Posted on 2005-04-20
Last Modified: 2013-12-12
I can use freetds and php to connect to mssql servers from the command line for example the following returns exactly what is expected:
[root@INTRA1 html]# cat testsql1.php
$hostname = "database";
$username = "sa";
$password = "";
$dbName = "mydatabase";

MSSQL_CONNECT($hostname,$username,$password) or DIE("DATABASE FAILED TO RESPOND.");
mssql_select_db($dbName) or DIE("Table unavailable");

$query = "SELECT * FROM dbo.cdp";

$result = mssql_query( $query );

for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
         $line = mssql_fetch_row($result);
         print( "$line[0] - $line[1] - $line[2] - $line[3] - $line[4]\n");

BUT trying to run the same code through a browser Apache returns the follwing:
Fatal error: Call to undefined function: mssql_connect() in /var/www/html/testsql1.php on line 7
Any help would be appreciated.
Question by:drdewm
    LVL 14

    Assisted Solution

    Are you sure the MSSQL PHP lib is installed?

    Undefined function usually means the given function has not been found, so it must be that the lib is not installed.

    You should check phpinfo() to make sure it is installed. If not, you can recompile with MSSQL as detailed in the manual:
    LVL 14

    Accepted Solution

    Also, just noticed this:
    $hostname = "database";

    Is that a valid hostname?
    LVL 4

    Assisted Solution

    You need to have the MSSQL library installed, indeed.

    To get the MSSQL functions to work, you have to compile PHP with --with-mssql,
    and FreeTDS should be compiled using --enable-msdblib.

    You can check your PHP configuration with the command phpinfo().

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now