Knowledges Base
www.shareknowledges.com
A Web Site To Share Knowledges With You
Home
Art and Entertainment
Around The World
Business & Finance
Dot Net Knowledges Base
Dota Allstars
Foreign Exchange (Forex)
Gaming
Products Review & Specs
Programming Articles
Technology
Usefull Links
Link Exchange
Health Beauty Fitness
My Links
San Pian Ye
HuaChao & June
Flash Games
Buy Direct From China
Tips ABC
Dota Guide
Dota Tips
China Resource
Reviews Centre
SK Reviews
Reviews & Tips
Programming Articles
Connecting to MS Access in PHP with a DSN
This script demonstrates connecting to a MS Access database in PHP with 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.
$dsnname = "testdsn";
Sets a variable to hold the name of the DSN to connect to. You should change this to your DSN name.
$username = "testuser";
$password = "testpass";
These are the username/password to use for the DSN. You should change these to your username and password.
$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.
$connection = odbc_connect( $dsnname , $username, $password );
Creates the connection to the database with the specified parameters.
if (!$connection) {
Checks if the connection was successful, if it wasn't then an error will print and the program will exit.
$cur = odbc_exec( $connection, "select * from $table" );
Executes the query specified in the second parameter on the database connection in the first parameter.
if (!$cur) {
Checks if the query was successful. If it wasn't, an error will be printed and the program will exit.
print "<table border=1><TR>";
The results of the database query will be printed in an HTML table.
for($loop = 0; $loop < odbc_num_fields($cur); $loop++){
$name = odbc_field_name($cur, $loop);
Gets the field name, the first parameter is the result of a query and the second parameter is the number of the field. Numbering starts at 0.
print "<TH>$name</TH>";
}
This loop will go through each of the fields in the table and print out the field name as the first row of the HTML table.
print "</TR>";
Ends the row in the HTML table.
while( odbc_fetch_row( $cur ) ) {
This will loop through all of the remaining rows in the result set and put the row in the variable $cur.
print "<TR>";
Starts a new row in the HTML table.
for($loop = 0; $loop < odbc_num_fields($cur); $loop++){
$fieldinfo = odbc_result($cur, $loop);
This retrieves the value of the field at the number specified in the second parameter.
print "<TH>$fieldinfo</TH>";
}
This loop will go through each of the fields in the table and print out the value in the HTML table.
print "</TR>";
Ends the row in the HTML table.
print "</TABLE>";
Ends the HTML table.
odbc_close( $connection);
Closes the connection to the database.
--Here is the full script, the filename should be: access-dsn.php--
<html>
<head>
<title>Connecting to a MS Access database with PHP with a DSN</title>
</head>
<body>
<?php
//Easy CGI Test script for connecting to an Access database in PHP with 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 name of the DSN
$dsnname = "testdsn";
// this is the username/password for the DSN
$username = "testuser";
$password = "testpass";
// this is the name of the table to display
$table = "testtable";
// --------------------------------------------
//create the connection
$connection = odbc_connect( $dsnname , $username, $password );
if (!$connection) {
print "Error in odbc_connect. Could not connect to DSN: $dsnname!";
exit;
}
// send a simple odbc query . returns an odbc cursor
$cur= odbc_exec( $connection, "select * from $table" );
if (!$cur) {
print "Error in odbc_exec(). Could not execute query!";
exit;
}
//print the column names
print "<table border=1><TR>";
for($loop = 0; $loop < odbc_num_fields($cur); $loop++){
$name = odbc_field_name($cur, $loop);
print "<TH>$name</TH>";
}
print "</TR>";
// fetch the succesive result rows
while( odbc_fetch_row( $cur ) ) {
//print each field in the table
print "<TR>";
for($loop = 0; $loop < odbc_num_fields($cur); $loop++){
$fieldinfo = odbc_result($cur, $loop);
print "<TH>$fieldinfo</TH>";
}
print "</TR>";
}
print "</TABLE>";
// close the connection. important if persistent connections are "On"
odbc_close( $connection);
?>
</body>
</html>
--End Script--
1/5/2008 1:59:19 AM
Category
Database Connection Strings
Comments 0
People who read this, also read...
Connecting to MSSQL in ASP.NET with a DSN
Connecting to MySQL in ASP.NET without a DSN
Connecting to MS Access in ASP.NET without a DSN
Connecting to MSSQL in Perl with a DSN
Connecting to MSSQL in Perl without a DSN
Back
© 2007 www.shareknowledges.com
HuaChao & June
San Pian Ye
Share Knowledges