The sqlsrv Extension Missing in XAMPP
You are attempting to connect CodeIgniter to a SQL Server database using the sqlsrv driver, but you are hitting a hard stop: Fatal error: Call to undefined function sqlsrv_connect(). This isn't a configuration issue within your CodeIgniter application; it is a fundamental missing dependency on the server side.
The PHP sqlsrv extension is not included by default in standard Windows distributions or XAMPP packages. When you set $db['default']['dbdriver'] = 'sqlsrv', the CodeIgniter database library attempts to load the native Microsoft ODBC driver for SQL Server via this extension. If the extension isn't compiled into the PHP interpreter running inside your httpd.exe process, the function simply does not exist in memory.
Installing the Extension
To resolve the undefined function error, you must manually install the sqlsrv extension. This differs from installing a standard Apache module; it requires placing the specific DLL file into your PHP extension directory and ensuring XAMPP's PHP configuration recognizes it.
- Download the Extension: Visit the official Microsoft documentation to find the correct version of the
php_sqlsrv_7_x.dll(adjusting the number for your PHP version, e.g., 7.4 or 8.x). You must download the version that matches your XAMPP PHP build architecture. - Locate the Extension Directory: Navigate to your XAMPP installation folder (typically
C:\xampp\php). Look for a directory namedext. If you do not see anextfolder, create it. Place the downloaded.dllfile into this directory. - Update
php.ini: Open the main configuration file, usually located atC:\xampp\php\php.ini. Find the line that reads;extension=sqlsrv(or sometimes just;extension=pdo_sqlsrv). Remove the semicolon to uncomment it:extension=sqlsrv - Restart Apache: After saving the file, you must restart the Apache server in XAMPP for changes to take effect.
Verifying the Installation
Before diving deeper into CodeIgniter logic, verify that PHP can actually see the function. Create a simple info.php file in your web root containing phpinfo();, or simply run this snippet in your controller before attempting a database load:
if (extension_loaded('sqlsrv')) {
echo "SQLSRV extension is loaded.";
} else {
echo "SQLSRV extension is NOT loaded.";
}
If the output confirms the extension is missing, restart Apache and check again. Once the sqlsrv_connect() function is recognized, the fatal error will disappear.
Addressing the Driver Configuration Error
Your source code snippet reveals a critical syntax error in how you are instantiating the database object. You are calling $this->db->load('sql_Test', TRUE);. The second argument to the load method expects an array of configuration options, not a boolean flag for the connection status. Furthermore, passing 'TRUE' as a string literal (if quotes were present) or a boolean will result in unexpected behavior when passed to the database driver initialization logic.
The correct approach is to either use the default configuration defined in your database/config/database.php file, or pass an associative array of overrides if you need specific settings:
// Correct usage
$db_obj = $this->load->database('default', TRUE);
The $db_obj->initialize() method call is also generally unnecessary after loading the database object in CodeIgniter 2.x or 3.x, as the connection is established immediately upon passing the second TRUE argument to the database() constructor. If you attempt to initialize a database object that was loaded without a connection (i.e., with FALSE), and then manually call initialize(), it will attempt to connect again. However, your logic block attempting to fallback to another database (yyy) based on a boolean return from initialize() is flawed because initialize() returns the database resource or connection object upon success, not a boolean status.
Trade-offs with XAMPP and SQL Server
Connecting to a remote SQL Server instance (indicated by the IP Adress in your config) from an Apache server running on XAMPP introduces potential latency issues not present when using MySQL locally. The sqlsrv driver relies on native Windows ODBC drivers, which can sometimes be finicky regarding network timeouts or firewall restrictions between your localhost environment and the remote database.
If you are building a development stack specifically for SQL Server, consider ensuring that the XAMPP distribution you chose actually supports it well. Some community builds of XAMPP include sqlsrv pre-installed to facilitate this exact workflow. If you are using the official XAMPP installer from Apache Friends, manual installation is required.
For production environments or when migrating legacy CodeIgniter applications to modern stacks like Laravel, note that Laravel's database abstraction layer handles these extension dependencies similarly but often provides more verbose error messages regarding missing extensions. However, for a CodeIgniter project specifically targeting SQL Server on Windows, ensuring the sqlsrv DLL is present and enabled in php.ini remains the single most important step to resolve connection initialization failures.