Home >Database >Mysql Tutorial >Why am I getting a 'Call to undefined function sqlsrv_connect()' error in my XAMPP setup when connecting to SQL Server?

Why am I getting a 'Call to undefined function sqlsrv_connect()' error in my XAMPP setup when connecting to SQL Server?

Barbara Streisand
Barbara StreisandOriginal
2025-01-03 19:11:39922browse

Why am I getting a

Debugging "Call to undefined function sqlsrv_connect()" Error in XAMPP for SQL Server Connection

Problem:

You encountered an error message "Fatal error: Call to undefined function sqlsrv_connect()..." when attempting to establish a database connection using the sqlsrv_connect() function.

Background:

The sqlsrv_connect() function is part of the SQLSRV extension for PHP, which allows you to connect to Microsoft SQL Server databases. This extension is not automatically installed in XAMPP versions 3.2.1 and above.

Solution:

To resolve this error, follow these steps:

  1. Download and Install SQLSRV Extension:

    • Visit the Microsoft Drivers for PHP for SQL Server download page: https://www.microsoft.com/en-us/download/details.aspx?id=20098
    • Download the appropriate extension file (e.g., SQLSRV32.EXE) for your version of PHP.
    • Choose the "C:xamppphpext" directory as the installation path.
  2. Uncomment or Append SQLSRV Extension in php.ini:

    • Open the php.ini file located in the XAMPP installation directory.
    • Search for the line containing "extension = php_sqlsrv_56_ts.dll" and uncomment it by removing the leading semicolon (;) if it exists.
    • If the line is not present, add it to the end of the php.ini file.
  3. Restart Apache in XAMPP:

    • Stop and start the Apache service in the XAMPP Control Panel.

Code Example:

After completing these steps, your code should look like the following:

<?php
$serverName = "jy4nij6vuy.database.windows.net,1433";
$connectionOptions = array("Database" => "robertfarb",
    "UID" => "robertFarb",
    "PWD" => "******");
$conn = sqlsrv_connect($serverName, $connectionOptions);

if ($conn === false) {
    die(print_r(sqlsrv_errors(), true));
}
?>

Additional Considerations:

  • Make sure you have the correct credentials (UID, PWD) for your SQL Server database.
  • If you encounter any other errors related to connecting to the database, refer to the official documentation for the specific error message.

The above is the detailed content of Why am I getting a 'Call to undefined function sqlsrv_connect()' error in my XAMPP setup when connecting to SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn