Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and Microsoft Access database - Connection and CRUD

I have no experience with access.

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

like image 692
Mask Avatar asked Oct 22 '09 07:10

Mask


People also ask

Can PHP connect to MS Access?

Can I access Microsoft Access databases? If you are running PHP on a Unix box and want to talk to MS Access on a Windows box you will need Unix ODBC drivers. » OpenLink Software has Unix-based ODBC drivers that can do this.

Can I use MS Access as database for my website?

Microsoft Access software lets you create a small database you can use on your website. You must first create a new database, then create the tables that store the website information. Access is beneficial for small websites and webmasters who are unfamiliar with database structures.

Can I use MySQL with Microsoft Access?

If you have existing data in an Access database, an Excel spreadsheet, or a delimited text file, this can be read into Microsoft Access and exported to your database on the MySQL server. Before you can use Access with MySQL, you must first have set up a Data Source for your MySQL database.

What is PDO connection in PHP?

To standardize and streamline development practices, PHP introduced PHP Data Objects (PDO) in PHP 5.1. These objects are used to setup PDO database connections. PDO is a database access layer which provides a fast and consistent interface for accessing and managing databases in PHP applications.


1 Answers

PDO

If you want to interface with an MS Access database using PHP, PDO is available for you.

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

When using PDO, due to the unified interface for DB operations, you have the opportunity to make your app more portable across various RDBMs systems. All you have to do is to provide the connection string to the PDO new instance and have the correct PDO driver installed.

As the result of this unified interface, your application can be easily ported from MS Access to MySQL, SQLite, Oracle, Informix, DB2, etc. which most certainly is the case if it ages enough.

Here's an insertion example:

<?php
try {
   // Connect, 
   // Assuming that the DB file is available in `C:\animals.mdb`
   $pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\animals.mdb;Uid=Admin");

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

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

    // close the database connection
    // See: http://php.net/manual/en/pdo.connections.php
    $pdo = null;
}
catch (PDOException $e) {
    echo $e->getMessage();
}

ODBC

In case you don't want to use PDO for some insane reasons, you can look into ODBC.

Here's an example:

<?php

if (! $conn = odbc_connect('northwind', '', '')) {
    exit("Connection Failed: $conn");
}

if (! $rs = odbc_exec($conn, 'SELECT * FROM customers')) {
    exit('Error in SQL');
}

while (odbc_fetch_row($rs)) {
  echo 'Company name: ', odbc_result($rs, 'CompanyName'), PHP_EOL;
  echo 'Contact name: ', odbc_result($rs, 'ContactName'), PHP_EOL;
}

odbc_close($conn);
like image 157
sepehr Avatar answered Nov 08 '22 12:11

sepehr