I am using Apache server version 2.4 and PHP version 8.2. I want to connect to SQL Server but get the following error:
Fatal error: Uncaught Error: Call to undefined function mssql_connect() in D:\Apache24\htdocs\mcd\configsql.php:6 Stack trace: #0 {main} thrown in D:\Apache24\htdocs\mcd\configsql.php on line 6
I have added this extension
to D:\Apache24\php\php.ini
:
extension=php_sqlsrv_82_ts_x64.dll
extension=php_pdo_sqlsrv_82_ts_x64.dll
The extension has also been installed correctly and appears in phpinfo()
, like this screenshot: https://prnt.sc/9UI5tQM6gFGl
I have also used the sqlsrv_connect()
function like this:
<?php
$myServer = "pcserver";
$myUser = "username";
$myPass = "password";
$myDB = "dbus";
$dbhandle = sqlsrv_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
$selected = sqlsrv_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB");
But got the error below:
Fatal error: Uncaught ArgumentCountError: sqlsrv_connect() expects at most 2 arguments, 3 given in D:\Apache24\htdocs\mcd\configsql.php:6 Stack trace: #0 D:\Apache24\htdocs\mcd\configsql.php(6): sqlsrv_connect('pcserver', 'username', 'password') #1 {main} thrown in D:\Apache24\htdocs\mcd\configsql.php on line 6
I have tried following the sqlsrv_connect
documentation on this link: https://www.php.net/manual/en/function.sqlsrv-connect.php.
I tried using a connection with Windows authentication until the username and password were like this:
<?php
$serverName = "192.168.0.1"; //serverName\instanceName
$connectionInfo = array( "Database"=>"mydbname", "UID"=>"sa", "PWD"=>"password");
$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));
}
?>
The code does not get results but gets the following error: https://prnt.sc/HQyaawaNh5x0
The functions mssql_connect()
and mssql_select_db()
are part of the old and unsupported mssql
PHP extension. If you want to connect to SQL Server instance using PHP Driver for SQL Server you need to use sqlsrv_connect()
, which replaces both functions. There is no sqlsrv_select_db()
function as part of the PHP Driver for SQL Server and changing the prefix mssql
in the functions names with sqlsrv
is not an option.
The sqlsrv_connect()
expects two parameters - $serverName
(a string specifying the name of the server to which a connection is being established) and $connectionInfo
(an associative array that contains connection attributes). The database in use is usually the first connection option. If you want to connect using SQL authentication, include username and password of the SQL Server login as additional connection options and ensure that your current SQL Server instance uses both SQL and Windows authentication:
<?php
// Connect to SQL Server instance using SQL authentication
$myServer = "pcserver";
$myUser = "username";
$myPass = "password";
$myDB = "dbus";
$options = array(
"Database" => $myDB,
"UID" => $myUser,
"PWD" => $myPass
);
$dbhandle = sqlsrv_connect($myServer, $options) or die ("Couldn't connect to SQL Server");
// Connect to SQL Server instance using Windows authentication
$myServer = "pcserver";
$myUser = "username";
$myPass = "password";
$myDB = "dbus";
$options = array(
"Database" => $myDB
);
$dbhandle = sqlsrv_connect($myServer, $options) or die ("Couldn't connect to SQL Server");
?>