SoFunction
Updated on 2025-04-09

Simple example of PHP using ADODB via COM

To implement the following functions, make sure that the com.allow_dcom option in   is set to true.

1. Preparation

Create a new ACCESS database and name it, then create a new table comtest in this database, including two fields: id and title, and finally insert some data at will.

2. Implement code

<?php
// It's just a database built
$db = 'd:\\wwwroot\\'; 

// Establish a connection and open
$conn = new COM('') or die('can not start Active X Data Objects');
//$conn->Open("Provider=.4.0; Data Source=$db");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");

// Execute query and output data
$rs = $conn->Execute('SELECT * FROM comtest');
?>
<table border="1">
<tr><th>ID</th><th>Title</th>
</tr>
<?php
while (!$rs->EOF) {
    echo '<tr>';
    echo '<td>'. $rs->Fields['id']->Value .'</td>';
    echo '<td>'. $rs->Fields['title']->Value .'</td>';
    echo '</tr>';
    $rs->MoveNext();
}
?>
</table>
<?php
// Release resources
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>