views:

470

answers:

2

I have no experience with access.

How to do update/insert/delete/select statement with and without $rs = new com("ADODB.RecordSet"); ?

+7  A: 

I have no clue about that $rs line! but if you want to connect & CRUD to a MS Access database using PHP, PDO is available for you. This PDO Introduction is also worth a look.

<?php
    try{
        $dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin");
    }
    catch(PDOException $e){
        echo $e->getMessage();
    } 
?>

Update:
While using PDO, you can make your application more portable due to it's unified interface for DB operations, All you have to do is providing the Connection String to the PDO new instance. So you can connect to MS Access, MySQL, SQLite, Oracle, Informix, DB2, etc. All you need is to have the correct PDO driver installed. take a look at this examples:

Insert Operation Example, Assuming that the DB file is available in C:\animals.mdb

<?php
try{
   // Connect
   $dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\animals.mdb;Uid=Admin");

    // INSERT data
    $count = $dbh->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");

    // echo the number of affected rows
    echo $count;

    // close the database connection
    $dbh = null;
}
catch(PDOException $e){
    echo $e->getMessage();
}
?>

I strongly recommend you to read that PDO tutorial & you'll be done ;) Also if you don't want to use PDO for some reasons, you can read a guide on ODBC Connections here. there's an example:

<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> 
Sepehr Lajevardi
can you be more specific about how to do CRUD operations?
Mask
Mask, there is an Insert operation right in there. Use the relevant select update and delete SQL operations.
Josh Smeaton
A: 

and first of all ..

" All you need is to have the correct PDO driver installed. " -> WHERE can I get one and install it ? on CentOS 5.5 i keep getting : could not find driver (as caught exception)

to make it a bit more challenging .. I dont know the actual mdb-file , instead i have data like for other sql-servers ..

$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=\\\\10.7.91.9\\".$DBank.";Uid=".$user.";Pwd=".$pass.";");

is all i have ...And I know the DB is on that Server and accessible from Windows

eagle275
Is this a question or an answer?
David-W-Fenton