Microsoft Access Database CRUD Operations Using PHP PDO

Microsoft Office is undoubtedly the most popular software with a combination of many utility packages for word processing, multimedia presentation making, spreadsheet data processing and data base management etc.

In Our previous articles, I have explained to connect with MS Excel/CSV/MS Word using PHP. In this article I am presenting a code snippet to implement CRUD operations on MS Access database files (.mdb file).

This is pretty simple and can be easily achieved by using PHP PDO classes.

PHP and Microsoft Access Database CRUD Operations

PHP PDO with taking Microsoft Access Driver as a default driver for connecting with MS Access files, fulfills our purpose. Microsoft Access Driver is the default driver used by Microsoft Office and one gets it installed automatically by installing MS Office on his PC.

Here is the sample code for retrieving rows from .mdb file:

$db_name = $_SERVER["DOCUMENT_ROOT"] . "/folder /customer.mdb"; //  Path to your .mdb file

if (!file_exists($db_ name)) 
{
    die("Could not find .mdb  file.");
}
try
{
$db_con  =  new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$ db_ name; Uid=; Pwd=;");
 
$sql  = "SELECT * FROM Customer_Table";   // table inside .mdb file

$result = $db_con  ->query($sql);
$row = $result->fetch();
echo "<pre>";
print_r($row);
}
} catch (PDOException $db_con) 

{
    echo 'Connection failed: ' . $db_con->getMessage();
}

Sample code for Inserting Rows into .mdb file:

$db_name = $_SERVER["DOCUMENT_ROOT"] . "/folder /customer.mdb";    //  Path to your .mdb file
if (!file_exists($db_ name)) {
    die("Could not find .mdb  file.");
}

$db_con  =  new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$ db_ name; Uid=; Pwd=;");

$email = '[email protected]';
$fname = 'test';
$lname = 'user';

$sql  = "INSERT INTO Customer_Table (Email_Address,First_Name,Last_Name) VALUES ('$email','$fname','$lname')";


$db_con->query($sql);
} catch (PDOException $db_con) {
    echo 'Connection failed: ' . $db_con->getMessage();
} 

Likewise, you can make other microsoft access database crud operations on .mdb files using PHP. Thanks

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *