Thursday, November 12, 2009

Connect to a MSSQL(SqlServer)

Even though LAMP is the most familiar compination we can try with other databases too in PHP. Here is a sample for connecting to SQLServer from PHP.

<?php
$myServer = "ServerName";
$myUser = "UserName";
$myPass = "Password";
$myDB = "DBName";


$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Connection Failed");
$selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open the db");
$query = "SELECT postid, postname, time";
$query .= "FROM Revolution-of-web";
$query .= "WHERE label='PHP'";

$result = mssql_query($query);

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

while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["postid"] . $row["postname"] . $row["time"] . "</li>";
}
mssql_close($dbhandle);
?>

No comments:

Post a Comment

...

Obstacles are those frightful things you see when you take your eyes off your goal.------> by Henry Ford