ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Microsoft Access Database Connectivity
Submitter: Daniel Hendricks (other recipes)
Last Updated: 2002/04/28
Version no: 1.0
Category: Databases

 

4 stars 1 vote(s)


Approved

Description:

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

Source: Text Source

<?
$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.



Add comment

Number of comments: 5

OLEDB in place of ODBC, Jason Brown, 2002/09/01
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/
Add comment

Agree, Morten Moe, 2004/07/23
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;");
Add comment

Richie Cahipe, 2006/05/17

Add comment

returned value, fred koehler, 2005/11/10
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){...}

2. 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
Add comment

Richie Cahipe, 2006/05/17

Add comment



Highest rated recipes:

1. Microsoft Access ...

2. main - python-like if ...

3. tinySendMail

4. Single Linked List

5. iPHP: Semi-interactive ...

6. DB_eSession PHP class ...

7. PHP MySQL Search Class

8. Pass Javascript arrays ...

9. Password generation

10. Microsoft Access ...




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.