Following is the sample PHP and MSSQL server connection script:


//SQL server details – Make necessary changes
$Server = “localhost”;
$User = “your_name”;
$Pass = “your_password”;
$DB = “example”;

//Database connection
$dbconn = mssql_connect($Server, $User, $Pass)
or die(“Couldn’t connect to SQL Server on $Server”);

//Select Database
$selected = mssql_select_db($DB, $dbconn)
or die(“Couldn’t open database $myDB”);

//declare the SQL statement that will query the database
$query = “SELECT * from test “;

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo “<h1>” . $numRows . ” Row” . ($numRows == 1 ? “” : “s”) . ” Returned </h1>”;

//display the results
while($row = mssql_fetch_array($result))
echo “<li>” . $row[“id”] . $row[“name”] . $row[“age”] . “</li>”;
//close the connection

Arvixe Web Hosting

The above code will work for SQL database which has three fields i.e. id, name and age.

Kailash Aghera

Sample PHP MSSQL connection script

Last Updated on

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.