ActiveState Code

Recipe 123709: Microsoft Access Database Connectivity


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

PHP
 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(); 
?>

Discussion

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.

Comments

  1. 1. At 7:08 p.m. on 1 sep 2002, Jason Brown said:

    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/

  2. 2. At 4:22 a.m. on 23 jul 2004, Morten Moe said:

    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;");

  3. 3. At 1:45 a.m. on 10 nov 2005, fred koehler said:

    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

  4. 4. At 12:54 p.m. on 17 may 2006, Richie Cahipe said:

  5. 5. At 12:54 p.m. on 17 may 2006, Richie Cahipe said:

Sign in to comment