Wednesday, April 4, 2012

ODBC connection With MS Access DataBase in PHP

<html>
<body>
<?php
//odbc conncetion with MS Access

// create database in ms access 
/* control pannel => Administrative Tools =>  Data Sources (ODBC) =>System DSN  => add => Microsoft Access Driver =>Finish

Create => 'name of database and select location' // open database and create table  */

// Configuration Variables **/ 

define('DB_NAME''db1.mdb');  // db1.mdb  database name 
define('DB_USER'''); 
define('DB_PASSWORD'''); 
$dbq=dirname($_SERVER['SCRIPT_FILENAME']). '/'.DB_NAME // database location 
   

 if  (!file_exists($dbq)) {  // if database not Exist
        die( 
"<br />Database Not Found."); 
    } 

$conn odbc_connect("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$dbqDB_USERDB_PASSWORD"SQL_CUR_USE_ODBC"); 

if (!
$conn){exit("Connection Failed: " $conn);} 


  
odbc_exec($conn,"insert into table1(id , name)values(1,'ffff')");  // insert query   // table1 = table name

  
$sql="SELECT * FROM table1"; $rs=odbc_exec($conn,$sql); 


if (!
$rs){exit("Error in SQL");}
echo 
"<table><tr>";
echo 
"<th>Name</th>";
echo 
"<th>Name</th></tr>";
while (
odbc_fetch_row($rs))
  {
  
$compname=odbc_result($rs,"name");
  
$conname=odbc_result($rs,"name");
  echo 
"<tr><td>$compname</td>";
  echo 
"<td>$conname</td></tr>";
  }
  
//odbc_exec($conn,"delete from table1"); // delete query , delete all record

odbc_close($conn);
echo 
"</table>"; ?>
</body>
</html>

2 comments:

  1. Amazing I used it and it worked perfectly.Thank you so much.

    Kareem

    ReplyDelete
  2. Nice replies in return of this matter with genuine arguments and explaining everything about that.


    Feel free to visit my weblog - skylights Cape Town

    ReplyDelete

If you Satisfied , Please Leave a comment