PHP 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:
1
2
3
4
5
$conn=odbc_connect('northwind','','');
 
$sql="SELECT * FROM customers";
 
$rs=odbc_exec($conn,$sql);
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.
The function takes two parameters: the ODBC result identifier and an optional row number:
1
odbc_fetch_row($rs)
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 code line below returns the value of the first field from the record:
1
$compname=odbc_result($rs,1);
The following example demonstrates how to first create a database connection, then a result-set, and then display the data in an HTML table:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<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>

PHP Include
PHP File
PHP Sessions
PHP Error