Computer Programming School -  Web School
Da Clock and Date Stuff
Page last modified: 02 July 2008

Source Code For Getting Data From Microsoft Access in PHP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Getting Data out of An Access Database Table From PHP</title>
</head>

<body>
<h2>Outputting A Table From Microsoft Access in PHP</h2>
Read <a href="http://www.w3schools.com/PHP/php_db_odbc.asp">http://www.w3schools.com/PHP/php_db_odbc.asp</a> for info on how to set up an ODBC Connection to a Microsoft Access database.
<h4>These are the courses in the table:</h4>
<?php

/*	***********************************************************
 *	Connect to A Microsoft Access Database -- Note that we have
 *	to Set up the connection with a DSN in the Control Panel 
 *	via Admin Tools beforehand! You DO NOT have to have a 
 *	user name and password -- I put them in to make the setup
 *	similar to what we do with MySQL
 *	***********************************************************/
$conn=odbc_connect('theAccessOne','Peter','daPassword');
if (!conn)
	{exit("Wiped out trying to connect: " . $conn);}
	

/*	*********************************************
 *	Set up the SQL Query and Run It
 *	Note that this is Microsoft SQL (not MySQL)!!
 *	(Since we are on a Microsoft database)
 *	*********************************************/	
$query = "SELECT * FROM courses";
$rs = odbc_exec($conn,$query);
if (!conn)
	{exit("Wiped out on the SQL Query");}
	

/*	******************************
 *	Set Up A Table For The Results
 *	******************************/	
echo "<table border='4' bgcolor=#00CCFF><tr>";
echo "<td>Course ID</td><td>Course Name</td><td>Program</td><td>Course Length</td><td>Fees</td></tr>";
while (odbc_fetch_row($rs))
	{$courseID = odbc_result($rs,courseID);
	 $courseName = odbc_result($rs,courseName);	 
	 $courseProgram = odbc_result($rs,courseProgram);
	 $courseLength = odbc_result($rs,courseLength);
	 $coursePrice = odbc_result($rs,coursePrice);
	 echo "<tr><td>" ."$courseID". "</td>";
	 echo "<td>" ."$courseName". "</td>";
	 echo "<td>" ."$courseProgram". "</td>";
	 echo "<td>" ."$courseLength". "</td>";	 
	 echo "<td align=right>" ."$".number_format("$coursePrice",2,'.',','). "</td></tr>";
	 }
echo "</table>";	 


/*	******************************
 *	Close The Connection Before We
 *	Bail Out -- Same as in MySQL
 *	******************************/
 odbc_close($conn);	 
?>


</body>
</html>