PHP info(): MSSQL Support

To check the PHP support for MSSQL we can use the function phpinfo(). To enable MSSQL support we have to uncomment the this line inside php.ini file.
extension=php_mssql.dll
If the support exists then you will get the details like this below.
mssql support of php in php info page

Example: Testing MSSQL Connection

<?php
$serverName = "localhost";
$connectionOptions = array("Database"=>"myDB", "Uid"=>"user", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn) {
    echo "Connected to MSSQL!";
} else {
    echo "Connection failed!";
}
?>

mssql_connect() connection ( < PHP 7 or old PHP )

$dbusername='username';
$dbpassword='password'; $servername='servername'; $link = mssql_connect ($servername,$dbusername,$dbpassword);

Example: Querying Data from MSSQL ( < PHP 7 or old PHP )

Using mssql_query() with mssql_connect()
$dbusername='';
$dbpassword='';
$servername='';
$link= mssql_connect ($servername,$dbusername,$dbpassword);
$query="select * from emp_m ";
$qt=mssql_query($query);
while($nt=mssql_fetch_array($qt)){
echo "$nt[emp_no]"." $nt[name]";
echo "<br>";}
Or using sqlsrv_query() ( for modern PHP versions)
<?php
$query = "SELECT * FROM employees";
$stmt = sqlsrv_query($conn, $query);
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    print_r($row);
}
sqlsrv_close($conn);
?>

Difference Between sqlsrv_query() and mssql_query() in PHP

sqlsrv_query(): Part of the SQLSRV driver, designed to connect PHP to Microsoft SQL Server. It supports advanced features like prepared statements, parameterized queries, and error handling. Recommended for modern PHP versions.

$conn = sqlsrv_connect($serverName, $connectionOptions);
$query = "SELECT * FROM Users";
$result = sqlsrv_query($conn, $query);

mssql_query(): Part of the old mssql extension, deprecated in PHP 7. Limited functionality compared to sqlsrv_query(), and no longer recommended for new development.

$conn = mssql_connect($serverName, $username, $password);
$query = "SELECT * FROM Users";
$result = mssql_query($query, $conn);
Introduction to PHP Collect php.ini settings by using ini_get() Guide to installation and How to write our first PHP Script

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer