閱讀: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);
?>
代碼0
將帶有DSN
ODBC Functions
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";
?>
什麼是錯誤? –
有什麼問題? – Robert
這實際上並未連接到數據庫以允許我提取登錄詳細信息 – Tasty