Connecting MsSQL database with PHP IIS7

405 views
asked Apr 29, 2012 in Database by SaraSX (2,770 points)
Hi there
Can anyone please explain to me how do I connect to MSSQL Database using PHP in IIS 7, I'm new to to PHP and I'm a bit confused here.
Any help would be really appreciated

Thanks

2 Answers

0 like 0 dislike
answered Apr 29, 2012 by TopNet (10,360 points)
Using sqlsrv_connect, you can connect to a MSSQL databse using the following code:
<?php

$conn_array = array (
            "UID" => "username",
            "PWD" => "password",
            "Database" => "database_name",
            "Encrypt" => 1,
            "TrustServerCertificate" => 1) ;

$conn = sqlsrv_connect('database_server' , $conn_array);

if ($conn)
    {
        echo 'MSSQL Connection successful';
    }
else
    {
        die( print_r( sqlsrv_errors(), true));
    }

sqlsrv_close( $conn );

?>
 
 
 If the connection is successful, it will return the message: "MSSQL Connection successful". If not, an error message will be displayed.
0 like 0 dislike
answered Apr 29, 2012 by WebGuru (9,280 points)
another method would be Using mssql_connect

this is the code that you can use

<?php

$uid = "username";
$db = "database_name";
$pwd = "password";
$host = "database_server";

$conn = mssql_connect($host, $uid, $pwd) or die('Could not connect')
    or die("Couldn't connect to SQL Server on $host");

$selected = mssql_select_db($db, $conn)
    or die("Couldn't open database $db");

echo "MSSQL Connection successful";

?>
...