Welcome, guest | Sign In | My Account | Store | Cart

This is an example of how you can pull data from a Microsoft Access database through ADO.

PHP, 18 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?
$conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 

// Microsoft Access connection string.
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\inetpub\\wwwroot\\php\\mydb.mdb");

// SQL statement to build recordset.
$rs = $conn->Execute("SELECT myfield FROM mytable");
echo "<p>Below is a list of values in the MYDB.MDB database, MYABLE table, MYFIELD field.</p>";

// Display all the values in the records set
while (!$rs->EOF) { 
    $fv = $rs->Fields("myfield");
    echo "Value: ".$fv->value."<br>\n";
    $rs->MoveNext();
} 
$rs->Close(); 
?>

This code is useful for those who want to use a Microsoft Access database with a PHP script. This code works on Microsoft Windows only, so you might want to check with your service provider before attempting to use this.

7 comments

Jason Brown 21 years, 7 months ago  # | flag

OLEDB in place of ODBC. to improve performance and reliability, it's recommended that OLEDB is used in place of ODBC

replace this line

$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\inetpub\wwwroot\php\mydb.mdb");

with

$conn->Open("Provider=Microsoft,.Jet.OLEDB.4.0; Data Source=C:\inetpub\wwwroot\php\mydb.mdb");

Atrax http://www.atrax.ws/

Morten Moe 19 years, 9 months ago  # | flag

Agree. OleDB sure is faster, at least in my experience. But i got problems with the connectionstring because I need to connect to a password protected ACCESS database. Anyway I thought I should post the solution in case others run into the same problems :) Here's the connection string I use:

$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Jet OLEDB:Database Password=thepassword;Data Source=E:\website\db.mdb;");

fred koehler 18 years, 5 months ago  # | flag

returned value. hi!

im kind of new with this code, so i have two questions:

  1. what value is returned when i changed something in the db? i need something like that:

$change = $db->Execute("UPDATE termintab SET titel='$ftitel', link='$flink WHERE newsid='$fid'");

if ($change = TRUE){...}

  1. how can i check if there is anything in one line of the db, like: $check = odbc_result($db, title);

with the $result-> thing

and what is the returned value here, like

if ($result = TRUE){...}

thanks for an answer!

burns

Richie Cahipe 17 years, 11 months ago  # | flag

Richie Cahipe 17 years, 11 months ago  # | flag

girishpalmah 15 years, 4 months ago  # | flag

Hi, I have used the code but I am getting the following error.

Fatal error: Class 'COM' not found in /demo/index.php on line 22

How can I fix it?

Ujwala 12 years, 2 months ago  # | flag

Hi, I am very new to php, I want to use dsn less oledb driver for Ms Access 2003, but can not find documentation for required objects and methods; e.g $rs->RecordCount(), getarray() etc.

Created by Daniel Hendricks on Sun, 28 Apr 2002 (MIT)
PHP recipes (51)
Daniel Hendricks's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks