![]() |
|
PHP ODBC Example. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | PHP with ODBC 2 |
|
|
Retrieving Records The odbc_fetch_row() function is used to return records from the result-set. This function returns true if it is able to return rows, otherwise it returns false. The function takes two parameters: the ODBC result identifier and an optional row number: odbc_fetch_row($resultSet) Retrieving Fields from a Record 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. The following code line returns the value of the first field from the record set: $compname=odbc_result($resultSet, 1); The code line below returns the value of a field called "city": $city=odbc_result($rs, "city"); Closing an ODBC Connection The odbc_close() function is used to close an ODBC connection as follows: odbc_close($conn); A practical ODBC Example The following example first creates an ODBC data connection, then retrieves data from the database using the earlier created ODBC data connection, and then displays the retrieved data in a table:
<html>
<body>
<?php
$conn=odbc_connect('northwind','','');
if (!$conn){
exit("Connection to the database Failed: " . $conn);
}
$sql="SELECT * FROM customers";
$resultSet=odbc_exec($conn, $sql);
if (!$resultSet){
exit("Error!");}
echo "<table><tr>";
echo "<th>Company</th>";
echo "<th>Contact</th></tr>";
while (odbc_fetch_row($resultSet))
{
$company=odbc_result($resultSet,"CompanyName");
$contact=odbc_result($resultSet,"ContactName");
echo "<tr><td>$company</td>";
echo "<td>$contact</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>
|