+ Reply to Thread
Results 1 to 1 of 1

Thread: PHP Open Database Connectivity (ODBC) Tutorial

  1. #1
    kailas is offline Newbie
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    PHP Open Database Connectivity (ODBC) Tutorial

    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:
    Code:
    $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:
    Code:
    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:
    Code:
    $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:

    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>
    (Taken from the resource PHP Tutorials)
    Last edited by James.H; 03-08-2010 at 01:06 PM. Reason: Replaced code tags with php tags

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Database Connectivity Problem?
    By MPL in forum PHP Development
    Replies: 1
    Last Post: 12-16-2010, 10:05 AM
  2. SQL Server database through ODBC.
    By ali0482 in forum C# Programming
    Replies: 4
    Last Post: 08-19-2010, 08:31 AM
  3. JAVA database connectivity
    By Ananta2010 in forum Java Help
    Replies: 3
    Last Post: 07-03-2010, 08:30 AM
  4. Tutorial: Java Database Connectivity
    By Jordan in forum Java Tutorials
    Replies: 1
    Last Post: 10-08-2008, 02:26 AM
  5. How to establish database connectivity ??
    By Patrick in forum Database & Database Programming
    Replies: 1
    Last Post: 10-08-2007, 05:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts