How to Fix 'Call to Undefined Function mssql_connect()' Error and Connect to MSSQL Database: A Step-by-Step Guide

If you've encountered the error message "Call to undefined function mssql_connect()", it means that you are attempting to connect to a Microsoft SQL Server database in PHP using the MSSQL extension, but the extension is not enabled or installed on your server. In this guide, we'll walk you through the steps to fix this error and establish a connection to your MSSQL database.

Step 1: Check if MSSQL Extension is Enabled

Before attempting to connect to your MSSQL database, you need to verify that the MSSQL extension is enabled on your server. You can do this by creating a PHP file with the following code:

<?php
phpinfo();
?>

Save the file as "phpinfo.php" and upload it to your server. Then, access the file in your web browser by visiting http://yourdomain.com/phpinfo.php. This will display a page with detailed information about your PHP configuration.

Scroll down to the "Registered PHP Streams" section and look for "mssql". If you don't see it, then the MSSQL extension is not enabled on your server. You can enable it by following the next step.

Step 2: Enable MSSQL Extension

To enable the MSSQL extension, you need to modify the PHP configuration file php.ini. Locate the file on your server and open it in a text editor.

Find the following line in the file:

;extension=php_mssql.dll

Remove the semicolon at the beginning of the line to uncomment it:

extension=php_mssql.dll

Save the file and restart the web server.

Step 3: Install MSSQL Drivers

If the MSSQL extension is already enabled on your server but you are still encountering the error message, it's possible that the drivers needed to connect to MSSQL are not installed. You can install them by following these steps:

Download the SQL Server Driver for PHP from the official Microsoft website: https://www.microsoft.com/en-us/download/details.aspx?id=20098

Extract the downloaded file and copy php_sqlsrv_XX_ts.dll and php_pdo_sqlsrv_XX_ts.dll (where XX is the PHP version you are using, e.g. 56 for PHP 5.6) to the ext directory of your PHP installation.

Open php.ini in a text editor and add the following lines at the end of the file:

extension=php_sqlsrv_XX_ts.dll
extension=php_pdo_sqlsrv_XX_ts.dll

Save the file and restart the web server.

Step 4: Test the Connection

After enabling the MSSQL extension and installing the drivers, you can now test the connection to your MSSQL database. Use the following code in a PHP file to connect to the database:

<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password" );
$conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $conn ) {
    echo "Connection established.<br />";
} else {
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}
?>

Replace "serverName\sqlexpress" with the name of your SQL Server instance, "dbName" with the name of your database, "username" with the database username, and "password" with the database password.

Save the file and upload it to your server. Then, access it in your web browser. If the connection is successful, you should see the message "Connection established." If not, you'll see an error message with more information about what went wrong.

FAQ

Q1: What should I do if I still can't connect to my MSSQL database after following these steps?

A1: Make sure that the database server is running and that you have the correct server name, database name, username, and password. If you're still having trouble, try reaching out to your hosting provider or IT department for assistance.

Q2: Is there a way to connect to MSSQL without using the MSSQL extension?

A2: Yes, you can use the PDO extension with the SQL Server driver for PDO to connect to MSSQL. You can find more information about this in the PHP documentation.

Q3: Why am I getting a "Class 'sqlsrv' not found" error?

A3: This error occurs when the SQL Server driver for PHP is not installed or not correctly configured. Follow the steps in Step 3 to install the drivers.

Q4: Can I connect to a remote MSSQL database?

A4: Yes, you can specify the IP address or hostname of the remote server in the $serverName variable in the connection code.

Q5: Do I need to install the MSSQL drivers if I'm using a Windows server?

A5: No, the MSSQL drivers are already included in the PHP installation on Windows servers.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.