System Information
CMS: Wordpress
Web Server: XAMPP
PHP Version: 5.5.30
MS Management Studio 17
Goal
Establish MSSQL Database connection using PHP
What has been done
- Downloaded SQLSRV Drivers
- Copied files php_pdo_sqlsrv_55_nts.dll and php_pdo_sqlsrv_55_ts.dll to the directory
C:\xampp\php\ext
- Added the following lines to the dynamic extensions part in the php.ini file:
extension=php_pdo_sqlsrv_55_ts.dll
and extension=php_pdo_sqlsrv_55_nts.dll
- Restarted Web Server
- Confirmed sqlsrv is listed in phpinfo()
Code
$serverName = "technology-pc\sqlexpress";
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"example_db");
$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));
}
Error
Call to undefined function sqlsrv_connect()
You have added the PDO variant of SQLSRV drivers to the extension list, but have not added the base drivers php_sqlsrv_55_ts.dll
.
Add to the php.ini:
extension=php_sqlsrv_55_ts.dll
or
extension=php_sqlsrv_55_nts.dll
Also, you really should be using either the Thread-Safe (_ts.dll
) or Non-Thread-Safe (_nts.dll
) versions of the driver, not both. I believe that, as you are using an Apache Server, you should be using the Thread-Safe versions. So you php.ini should have:
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
You probably edited the wrong php.ini file. Check the right php.ini file with php info.
You can use this script:
<?php echo phpinfo(); ?>
Or if you have CLI access type php -i to get the info listed.
Check for the right path of your php.ini file.
Try below code to connect mssql database
$server = 'dburl.com\MSSQLSERVER, 1433';
$username = 'uname';
$password = 'password';
$connectionInfo = array( "Database"=>$database, "UID"=>$username, "PWD"=>$password,"ReturnDatesAsStrings"=>true);
$conn = sqlsrv_connect( $server, $connectionInfo);