Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example PHP SQL ODBC

Related Tutorials


 
 

Share on Google+Share on Google+

PHP SQL ODBC

Advertisement
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:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 20, 2009

Related Tutorials

Discuss: PHP SQL ODBC  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com