Early PHP applications commonly used mssql_connect, mssql_init, and mssql_bind. Those functions belonged to an obsolete extension and should not be copied into a new application.
Choose a supported driver
For PHP with Microsoft SQL Server, evaluate the Microsoft Drivers for PHP for SQL Server (SQLSRV or PDO_SQLSRV), matching PHP, operating system, and SQL Server versions.
Example: SQLSRV stored procedure call
$connection = sqlsrv_connect($server, [
"Database" => "TrainingDB",
"UID" => getenv("DB_USER"),
"PWD" => getenv("DB_PASSWORD")
]);
if ($connection === false) { throw new RuntimeException("Connection failed"); }
$id = 17;
$params = [[$id, SQLSRV_PARAM_IN]];
$stmt = sqlsrv_query($connection, "{CALL dbo.GetCustomer(?)}", $params);
if ($stmt === false) { throw new RuntimeException("Query failed"); }
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo htmlspecialchars((string)$row["CustomerName"], ENT_QUOTES, "UTF-8");
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($connection);
Procedure contract
The example assumes dbo.GetCustomer accepts an integer parameter. For output parameters and result sets, consult the current driver documentation, since binding directions and buffer types matter.
Security essentials
Use least-privilege accounts, encrypted connections with valid certificates, and server-side input validation. Never return raw database error details to website visitors.
AI, sources and reuse: This is an AI-generated rewrite informed by the original Open Education post (#70089), with additional explanations and examples. The new article text may be copied, adapted, and republished with or without a reference to this site. Third-party resources and trademarks keep their own rights. AI can make mistakes. Verify version support, code correctness, and security practices against Microsoft documentation, and test with nonproduction data first.
