Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I connect to an Access(mdb) database?

Database (Microsoft)

How do I connect to an Access(mdb) database?

by  Karl Blessing  Posted    (Edited  )

This quick tutorial by example, assumes that you have PHP setup on a Windows2000 or NT machine using IIS4+


A quick example by source on how, to demostrate how, I wrote a little quick PHP version of the new postings on my site, (most of my site is done in ASP, I myself am just learning some PHP , I like the structure)

Code:
<HTML>
<BODY bgcolor=#FFFFFF>
<Table border=1 width="100%" bgcolor="darkgray" cellpadding=0 cellspacing=0>
<?
$conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 
//creates the connection object
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\WWWRoot\\kb244com\\www\\_private\\database\\SiteDb2k.mdb;Mode=ReadWrite;Persist Security Info=False");
//opens a connection using a connection string, for some
//people "DSN=name" will suffice if you are using a dNS connection
$rs = $conn->Execute("Select * from Postings order by PostDate DESC");
//opens a recordset from the connection object
    while (!$rs->EOF) {
    //keep looping until end of file
    ?>
	<? $fv = $rs->Fields("PostDate"); ?>
        //in ASP the date comes back correctly for a
        // Date/time type, not sure why it comes back different
        // in PHP
	<TR>
	<TD width="20%" align=left bgcolor=#B5BED6 nowrap><b><? echo $fv->value; ?></b></TD>
	<? $fv = $rs->Fields("PostTopic"); ?>
	<TD align=center bgcolor=#B5BED6><i><? echo $fv->value; ?></i></TD>
	</TR>
	<TR>
	<TD colspan=2 align=left bgcolor="lightgrey">
	<? $fv = $rs->Fields("Post"); ?>
	<? 
	echo ereg_replace( "\n", "<br>", $fv->value );
	?>
	</TD>
	</TR>
	<TR>
	<? $fv = $rs->Fields("Signature"); ?>
	<TD colspan=2 align=right bgcolor=#B5BED6><? echo $fv->value; ?></TD>
</TR>
<tr>
	<td colspan=2>á</td>
	</tr>
   <?
    $rs->MoveNext();
    } 
    $rs->Close(); 
?>
</Table>
</Body>
</HTML>

there you have it a simple connection to an Access database, just make sure that if the database is stored locally on the web, you must put it in a folder with write permision so that it can create it's temporary files.

It is also advisible to place it in the _private folder, so that global webvieiwers cant just download the database (any other folder and the person only needs to do http://www.kb244.com/thefile.mdb to get your database)

Also if anyone of you have ideas on how to improve upon this, let me know, as it would benefit me.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top