Home Sql Mysql-example PHP SQL ODBC



PHP SQL ODBC
Posted on: January 20, 2009 at 12:00 AM
This example illustrates how to create an ODBC Connection for php application.

PHP SQL ODBC

     

This example illustrates how to create an ODBC Connection for php application.

Follow the given steps to create an ODBC connection to a MS Access Database: 

 

 

 

 

 

 

  1. Go Control Panel->Administrative Tools
  2. Double-click on the Data Sources (ODBC) icon inside.
  3. Select System DSN tab.
  4. Click on Add.
  5. Select the Microsoft Access Driver.
  6. Click Finish.
  7. Click Select to locate the database.
  8. Give Data Source Name (DSN).
  9. Click OK.

Now we can use the DSN when connecting to the database. In the given example below, 'myodbc' is the DSN for the database. Connection from database is created using odbc_connect() and query "SELECT * FROM emp"  is executed using odbc_exec(). The result is fetched using odbc_fetch_row().

Source Code of sqlodbc.php 

<html>
<body>
  <?php
  $conn=odbc_connect('myodbc','','');
  if (!$conn){
  exit("Connection Failed: " . $conn);
  }
  
  $sql="SELECT * FROM emp";
  $rs=odbc_exec($conn,$sql);

  if (!$rs){
  exit("Error in SQL");
  }

  echo "<table><tr>";
  echo "<th>Emp_ID</th>";
  echo "<th>Name</th></tr>";

  while (odbc_fetch_row($rs)){
  $id=odbc_result($rs,"id");
  $name=odbc_result($rs,"name");
  echo "<tr><td>$id</td>";
  echo "<td>$name</td></tr>";
  }
  odbc_close($conn);
  echo "</table>";
  ?>

</body>
</html>

Download Source Code

Output:

Related Tags for PHP SQL ODBC:
phpcapplicationodbcioconnectionthisappcreateconnectforexampletoexameillimtrcadbcdbeshphowpprateratescatxaxampsatisllmpleastrssthbcstatiapicaicapleplono


More Tutorials from this section

Ask Questions?    Discuss: PHP SQL ODBC  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.