9/03/2011

Ubuntu : PHP & MSSQL Server

จะเขียน script PHP จาก Ubuntu ให้ติดต่อไป MSSQL Server ต้องติดตั้ง package เพิ่มเติม ดังน้ัี

sudo apt-get install php5-mssql

Code Test :


<?php
$myServer = "server";
$myUser = "user";
$myPass = "password";
$myDB = "database";
 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");
 
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");
 
//declare the SQL statement that will query the database
$query = "SELECT ItemId ";
$query .= "FROM InventTable ";
 
//execute the SQL query and return records
$result = mssql_query($query) or die("Couldn't 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["ItemId"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

No comments:

Post a Comment