Connecting to MS Access in PHP without a DSN

This script demonstrates connecting to a MS Access database in PHP without a DSN. The script will make the connection and then output all the rows of a specified table. Any lines beginning with a // are comments.

$db_location = "c:websitesyoursite22yoursite.comdb est.mdb";
Sets a variable to hold the physical path to the database. You should change this to the path for your database.

$table = "testtable";
Sets a variable to hold the name of the table to be displayed by the script. This should be changed to your table name.

$userid = "";
$password = "";

These are the username/password to use for the database. These are only necessary if your Access database has a username/password set.

$dbc = new COM("ADODB.Connection");
Creates a new ADO object for the connection.

$connstr = "DRIVER={Microsoft Access Driver (*.mdb)};";
$connstr .= "DBQ=$db_test;uid=$userid;pwd=$password;";

Creates the connection string from the specified parameters.

$dbc->open($connstr);
Attempts to connect to the database.

$rs = $dbc->execute("SELECT * FROM $tablename");
Executes the specified query on the database.

if(!$rs->eof()){
If there was a result from the query.

print "<TR>";
Starts a new HTML row.

$numfields = $rs->fields->count;
Gets the number of fields in the table.

for($loop = 0; $loop < $numfields; $loop++){
//print the name of each field
$fld = $rs->fields($loop);
print "<TH>$fld->name</TH>";
}

Loops through each field and prints the name of the field as the first row in the HTML table.

print "</TR>";
Ends the HTML row.

while(!$rs->eof()) {
Goes through the remaining rows from the result set.

$numfields = $rs->fields->count;
Gets the number of fields in the row.

print "<TR>";
Starts a new row in the HTML table.

for($loop = 0; $loop < $numfields; $loop++){
//print each field's value
$fld = $rs->fields($loop);
print "<TH>$fld->value</TH>";
}

Loops through each field in the row and prints the value in the HTML table.

print "</TR>";
Ends the HTML row.

$rs->movenext();
Moves to the next row in the result.

print "</TABLE>";
Ends the HTML table.

$dbc->close();
Closes the database connection.

--Here is the full script, the filename should be: access-dsnless.php--

<html>
<head>
<title>Sample script for accessing a MS Access database in PHP without a DSN</title>
</head>
<body>

<?php

//Easy CGI Test script for connecting to an Access database in PHP without a DSN
//This script will connect to the database and output the results in an HTML table

// Change these values to point to your database
// --------------------------------------------
// this is the physical location of the database
$db_location = "c:websitesyoursite22yoursite.comdb est.mdb";
// the name of the table to display
$tablename = "testtable";
// username and password for the database
$userid = "";
$password = "";
// ----------------------------------------------

//create the connection string
$dbc = new COM("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)};";
$connstr .= "DBQ=$db_test;uid=$userid;pwd=$password;";

$dbc->open($connstr);

//execute a query
$rs = $dbc->execute("SELECT * FROM $tablename");

//print the results to a table
print "<TABLE BORDER=1>";

//print the column names
if(!$rs->eof()){
print "<TR>";
$numfields = $rs->fields->count;
for($loop = 0; $loop < $numfields; $loop++){
//print the name of each field
$fld = $rs->fields($loop);
print "<TH>$fld->name</TH>";
}
print "</TR>";
}

//while not the end of the result set
while(!$rs->eof()) {
$numfields = $rs->fields->count;
print "<TR>";
for($loop = 0; $loop < $numfields; $loop++){
//print each field's value
$fld = $rs->fields($loop);
print "<TH>$fld->value</TH>";
}
print "</TR>";
//move to next row
$rs->movenext();
}

print "</TABLE>";

//dont forget to close connection
$dbc->close();

?>
</body>
</html>

--End Script--

 

<< Back

 
 
 
Copyright © 2004 Creative Brain   || Home | Digital Photo Album | Online Store | Template Depot | Career | Help & Support | Contact Us