阅读: http: //php.net/manual/en/function.mssql-connect.php
下面是连接到 MSSQL Server 数据库的代码
//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 id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";
//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["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
与 DSN 连接
ODBC 函数
DSN代表“数据源名称”。这是一种为数据源分配有用且易于记忆的名称的简单方法,这些数据源可能不仅限于数据库。
在下面的示例中,我们将向您展示如何使用 DSN 连接到名为“examples”的 MSSQL Server 数据库并从表“cars”中检索所有记录。
<?php
//connect to a DSN "myDSN"
$conn = odbc_connect('myDSN','','');
if ($conn)
{
//the SQL statement that will query the database
$query = "select * from cars";
//perform the query
$result=odbc_exec($conn, $query);
echo "<table border=\"1\"><tr>";
//print field name
$colName = odbc_num_fields($result);
for ($j=1; $j<= $colName; $j++)
{
echo "<th>";
echo odbc_field_name ($result, $j );
echo "</th>";
}
//fetch tha data from the database
while(odbc_fetch_row($result))
{
echo "<tr>";
for($i=1;$i<=odbc_num_fields($result);$i++)
{
echo "<td>";
echo odbc_result($result,$i);
echo "</td>";
}
echo "</tr>";
}
echo "</td> </tr>";
echo "</table >";
//close the connection
odbc_close ($conn);
}
else echo "odbc not connected";
?>