Hi All!
I present to you PHP (Open Database Connectivity ) ODBC.
One of PHP's strengths is database connectivity. PHP supports a large number of databases and simplifies access via a unified ODBC interface. This provides a number of useful functions.
The following example creates a connection to a DSN called northwind, with no username and no password. It then creates an SQL and executes it:
The odbc_fetch_row() function is used to retrieve records from the result-set. This function returns true if it is able to return rows, otherwise false.Code:$conn=odbc_connect('northwind','','');
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
The function takes two parameters: the ODBC result identifier and an optional row number:
The odbc_result() function is used to read fields from a record. This function takes two parameters: the ODBC result identifier and a field number or name.Code:odbc_fetch_row($rs)
The code line below returns the value of the first field from the record:
The following example demonstrates how to first create a database connection, then a result-set, and then display the data in an HTML table:Code:$compname=odbc_result($rs,1);
(Taken from the resource PHP Tutorials)Code:<html>
<body>
<?php
$conn=odbc_connect('northwind','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
{
$compname=odbc_result($rs,"CompanyName");
$conname=odbc_result($rs,"ContactName");
echo "<tr><td>$compname</td>";
echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>
Last edited by James.H; 03-08-2010 at 01:06 PM. Reason: Replaced code tags with php tags
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks