Windows Server2008下PHP7.2.4+Apache2.4调用srv_connect()报错求助
Hey there, let's fix that "call to undefined function srv_connect()" error you're hitting when trying to connect to SQL Server with CodeIgniter on Windows Server 2008. This issue almost always boils down to missing or misconfigured SQL Server extensions for PHP—here's a step-by-step breakdown to get you sorted:
PHP 7.2 requires a specific version of the SQL Server drivers. You'll need the thread-safe (TS) versions of both php_sqlsrv and php_pdo_sqlsrv that match your PHP architecture (x86 or x64) and PHP 7.2. These are part of Microsoft's official SQL Server Driver for PHP.
Make sure you download the right package—look for files named something like php_sqlsrv_72_ts.dll and php_pdo_sqlsrv_72_ts.dll.
Copy the downloaded .dll files into your PHP installation's ext directory (usually something like C:\PHP\ext if you installed PHP to the root of C:).
Open your php.ini file (found in your PHP install directory) and add these two lines to enable the extensions:
extension=php_sqlsrv.dll extension=php_pdo_sqlsrv.dll
Double-check that your extension_dir setting is pointing correctly to the ext folder. It should look like either:
extension_dir = "ext"
Or the full absolute path:
extension_dir = "C:\PHP\ext"
Don't skip this! Changes to php.ini won't take effect until you restart your Apache server. You can do this via the Windows Services console (search for "Services" in the start menu) or using Apache's own control panel.
Create a simple PHP file named phpinfo.php in your WWW directory with this content:
<?php phpinfo(); ?>
Access it via your browser (e.g., http://your-server/phpinfo.php) and search for sqlsrv and pdo_sqlsrv. If you see dedicated sections for both, the extensions are loaded correctly.
Head to your CodeIgniter application's application/config/database.php file and make sure your database settings use the sqlsrv driver. Here's a sample config to reference:
$db['default'] = array( 'dsn' => '', 'hostname' => 'your_sql_server_hostname_or_ip', 'username' => 'your_sql_server_username', 'password' => 'your_sql_server_password', 'database' => 'your_target_database', 'dbdriver' => 'sqlsrv', // This is critical—don't use mssql or other drivers! 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
The SQL Server PHP extensions depend on the Microsoft ODBC Driver for SQL Server. For Windows Server 2008, you'll need Microsoft ODBC Driver 13.1 or later (older versions don't support PHP 7.2). Install this driver if you haven't already—without it, the PHP extensions won't load properly.
If you've followed all these steps and still run into issues, double-check that your PHP 7.2.4 build is indeed thread-safe (Apache typically uses TS builds), and that all downloaded extensions match your PHP's architecture (x86 vs x64).
内容的提问来源于stack exchange,提问作者Fazal Afridi

