Resolving SQL Server Connectivity Errors in PHP on XAMPP
The error Call to undefined function sqlsrv_connect() is a classic indicator of a mismatch between your PHP environment and the Microsoft ODBC drivers for SQL Server. In the context of XAMPP, this issue almost always stems from how the extension is loaded rather than missing DLL files alone. The source material shows an attempt to connect using sqlsrv_connect(), but with PHP 7.2.6 running on a Windows machine (implied by XAMPP), the mere presence of php_sqlsrv_72_ts.dll and php_pdo_sqlsrv_72_ts.dll in the ext folder is insufficient without proper configuration.
To resolve this, you must explicitly enable the extensions in your PHP configuration. Locate the php.ini file within your XAMPP installation directory (typically under \xampp\php). If you do not see an entry for extension=php_sqlsrv.dll and extension=php_pdo_sqlsrv.dll at the bottom of the file, add them manually. Ensure there are no preceding semicolons (;) commenting out these lines. The correct syntax should look like this:
extension=php_sqlsrv.dll
extension=php_pdo_sqlsrv.dll
Once saved, restart the Apache server via the XAMPP Control Panel to ensure the new configuration takes effect. Without this step, PHP will ignore the DLLs regardless of where they are placed.
Beyond enabling the extensions, you must verify that the required ODBC driver is installed on the system. The sqlsrv extension relies on the Microsoft ODBC Driver for SQL Server. This is often not included by default in a standard Windows installation or even within some XAMPP bundles. You can check if the driver is present by running a command prompt as administrator and executing:
odbcinst -q -d | findstr "SQL Server"
If this returns nothing, download the latest Microsoft ODBC Driver from the official Microsoft website and install it. The sqlsrv extension generally requires the "ODBC Driver 17 for SQL Server" or a compatible version depending on your server setup. Mismatched driver versions are a frequent cause of silent failures where the code loads but throws runtime errors later in the execution flow.
Regarding the connection string provided in the source material, there is a critical architectural detail to address. The code attempts to connect to INDO-SERV\SQLEXPRESS,1443. SQL Server instances typically listen on port 1433 by default, not 1443 unless explicitly configured otherwise. If your listener is indeed set to 1443, ensure that the Windows Firewall allows inbound traffic on that specific port for the SQL Server service account.
Furthermore, the connectionInfo array in the source code uses named parameters (UID, PWD, Database). While this works, it is important to note how sqlsrv handles authentication modes. If your SQL Server instance requires Windows Authentication (Integrated Security), you must change the connection string and remove credentials from the array:
$connectionInfo = array("Database" => $databaseName, "Return Dates as Strings" => false);
// Note: Do not include 'UID' or 'PWD' if using Integrated Security
If you continue to face issues after ensuring the extensions are loaded and drivers are installed, consider switching to PDO. The pdo_sqlsrv driver offers better portability if you plan to move your application from XAMPP to a production environment later. It also provides more consistent error handling compared to the procedural sqlsrv_* functions.
For developers looking to manage complex database migrations or enterprise setups involving Laravel, resources like https://laravelcompany.com often provide detailed guides on configuring drivers for specific frameworks, ensuring your stack remains robust across different hosting environments. However, for a standalone XAMPP setup, sticking to the native sqlsrv extension is perfectly viable once configured correctly.
Finally, always check the Apache error logs located in \xampp\apache\logs\error.log if the connection still fails. Sometimes, even if PHP reports a successful load of the function, underlying DLL dependencies or missing system libraries can cause the function to fail silently until runtime, at which point sqlsrv_errors() will provide specific diagnostic details about driver incompatibilities.