Design and Development > PHP

Sample PHP MSSQL connection script

(1/1)

Kailash:
As of PHP 5.3, default MSSQL driver is no longer available in PHP. As an alternative, you can use PDO_SQLSRV Driver to connect to your MSSQL database. Before you use this extension, you will have to enable it from your PHP configuration. You can refer the following URL for sample code:

http://msdn.microsoft.com/en-us/library/ff754357%28v=sql.105%29.aspx

For native php_msql diver, the following script should work:


--- Code: ---<?php
$Server = "localhost";
$User = "your_name";
$Pass = "your_password";
$DB = "examples";

//connection to the database
$dbconn = mssql_connect($Server, $User, $Pass)
  or die("Couldn't connect to SQL Server on $Server");

//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT name 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 "<br>" . $row["name"];
}
//close the connection
mssql_close($dbconn);
?>
--- End code ---

If you are using PHP MSSQL driver, you can refer Sample PHP MSSQL connection script using PHP MSSQL driver.

Navigation

[0] Message Index

Go to full version