Tutorial:
Database - PHP - Display Security Users With Access Database
Dr.
Thomas E. Hicks
Computer Science
Department
Trinity University
IIS 5 Install on Windows XP Pro
IIS 5 Installation on Windows 2000 Pro/Server
It might even help to read a tutorial that describes the need for Database Web Applications.
Database Need For Web Applications
You may use the basic ideas of this tutorial for either the ODBC connection or the MapPath connection..
Database - PHP - ODBC Connections
Database - PHP - RealPath Connections
For purposes of discussion, let us suppose that an Access Database, called Security.mdb, resides at the root of drive C. (See Below!) When a database is installed on the web server, the necessary ODBC drivers are also installed. If the web server does not have the database installed, then the ODBC drivers will have to be downloaded and installed; this process differs from database to database and is beyond the scope of this paper. Microsoft Access and the appropriate ODBC drivers have been installed on the server illustrated below.

The Security Database is opened and the Tables tab is selected; in order to keep our example simplistic, the Security Database has only a single table, called Users. The contents of our very simple Users table can be seen below.

Each record in the Users table contains fields Name, No, Password, and IDNo; these will be needed later.

The datatypes of each field may be seen below.

The Security Database above was is ultra trivial application which had only one database table. Relational databases often have many tables; this is true of Access databases as well. This database may be downloaded.
We are going to place all of the PHP files in folder C:\Inetpub\wwwroot\PHP. We could place the database files anywhere on the drive. For security reasons, it is not a good idea to place them in the wwwroot directory.

For purposes of this tutorial, we shall assume that the Security database files are located in C:\Security. For purposes of this tutorial, we shall assume that the Security database is located in C:\.
File adodb.inc.php includes many of the database PHP4 extensions to php. File adodb-time.inc.php is used by adodb.inc.php. These should be placed in the web site. I have chosen to place them in directory ADODB for purposes of this tutorial..

There is PHP support for many databases; among them are Oracle, informix, mysql, ibase, cvc, postgres, sybase, foxpro, etc. Each of these shall require its own driver. Although you shall not neeed all of the drivers, I have placed a collection that I downloaded from the Internet in directory Drivers. See Below! These should be placed in the web site. I have chosen to place them in directory ADODB for purposes of this tutorial..

You might want to download your own files from the Internet, but for sake of simplicity, I have included those used in the tutorial in the zipped file below; place them in a folder, called ADODB, at the root of your IIS directory. (See Below!)

<?PHP #========================================================================= #========================================================================= #==== DisplaySecurityUsers.php ==== #========================================================================= #==== ==== #==== Purpose : Display all of the information about all of the ==== #==== Users in the Security database using a RealPath ==== #==== connection to the Security.mdb that is three ==== #==== levels up at C:\ ==== #==== ==== #==== Written By : Dr. Thomas E. Hicks Date: 08/1/2003 ==== #========================================================================= #========================================================================= |
The documentation block provides a brief statement of purpose. Immediately following the documentation block are declarations for all of the variables used on this page. Blocks of PHP code begin with <?PHP and end with ?>.
#--------------------------------------------------------------------------
# Include The Database Utilities
#--------------------------------------------------------------------------
Include('../adodb/adodb.inc.php');
|
This line of PHP code includes some of the PHP functions essential to database access.
#-------------------------------------------------------------------------- # Declarations & Initializations #-------------------------------------------------------------------------- $Counter = 1; |
This line of PHP creates a numeric variable, called Counter, and initializes it to 1.
#--------------------------------------------------------------------------
# Access Database RealPath Connect To The Server
#--------------------------------------------------------------------------
#------------------------ Create A Connection Object ----------------------
$Conn = New COM("ADODB.Connection");
|
The block of code above creates a new connection object, called Conn, that shall be used to connect to the Security database on the server.
#------------------------------ Absolute Path -----------------------------
$ConnStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" .
RealPath("C:\Security.mdb");
#------------------------------ Relative Path -----------------------------
$ConnStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" .
RealPath("../../../Security.mdb");
|
Variable ConnStr contains part of the connection arguments which shall enable the Conn object to link to the Security database. You may establish the path with an absolute connection or with a relative connection; you need not include both!
#---------------------------------- Connect ------------------------------- $Conn->Open($ConnStr); |
Open the connection to the Security.mdb database.
#--------------------------------------------------------------------------
# Read All Of The User Names Into The RecordSet
#--------------------------------------------------------------------------
#--------------------------- Traditional SQL Query ------------------------
$UserSQL = ' SELECT * ' .
' FROM Users';
|
Create a query! UserSQL is the traditional SQL command to read all of the information in the Users table of the Security.mdb.
#------------- Create A Record Set That Contains Results Of Query --------- $RecordSet = $Conn->Execute($UserSQL); |
Fill the RecordSet with all of the information returned from the SQL query.
#------- If No Record Set Was Generated, Print Out The Error Message ------
If (!$RecordSet)
{
Print "---------------- Unable To Create Record Set ---------------";
Print $Conn->ErrorMsg();
}
|
This will display some of the errors trapped by an unsuccessful query; depending upon the error, you may get one of those ugly white pages of gobbly-goo!
Else
#------- If There Are No Records In The Record Set, Display Message -------
If ($RecordSet->EOF)
Print "----- There Are No Records That Match This Query -----";
|
If there are no records in the record set, the record set pointer will be at the end of file; display a message to this effect.
#------------------ There Are Records To Display --------------------------
Else
{
$Name = $RecordSet->Fields(0);
$No = $RecordSet->Fields(1);
$Password = $RecordSet->Fields(2);
$ID = $RecordSet->Fields(3);
?>
|
There are records to display. Fill variable Name with the information in the first field of the the current record in the record set. Fill variable No with the information in the second field of the the current record in the record set. Fill variable Password with the information in the third field of the the current record in the record set. Fill variable ID with the information in the fourth field of the the current record in the record set. The exact order can be determined by examining the fields within the database table shown earlier. See Below!
![]()
<HTML><HEAD> <TITLE>Dr. Thomas E. Hicks - DisplaySecurityUsers.php </TITLE> </HEAD> |
The HTML places Dr. Thomas E. Hicks - DisplaySecurityUsers.php in the browser title bar.
<BODY TEXT = "#000000"
BGCOLOR = "#000000"
VLINK ="#000000"
ALINK ="#000000"
BACKGROUND ="Paper.jpg">
|
This HTML code above defines the page background, the default text color, the default background color, and the default link colors.
<CENTER> <FONT FACE="Arial" SIZE="4"> DisplaySecurityUsers.php <BR> Written By<BR> Dr. Thomas E. Hicks</font><P> </CENTER> <HR> |
The HTML code above creates the following commercial at the top of the page.
DisplaySecurityUsers.php
Written By
Dr. Thomas E. Hicks
<!-- ================================================================ -->
<!-- Display Users In The Table -->
<!-- ================================================================ -->
<font face="System" size="3" color="#FFFFFF">
<TABLE BORDER = "5"
CELLPADDING = "4"
CELLSPACING = "4"
STYLE = "border-collapse: collapse"
BORDERCOLOR = "#800000"
BGCOLOR = "#FFFFFF"
WIDTH = "100%"
ID = "UserTable">
|
Although it is not essential, we are going to display the information from the database in a traditional HTML table. The code above establishes the format for this table.
<!-- ================================================================ --> <!-- Title Bar Row --> <!-- ================================================================ --> <TR><TD ALIGN = "Right"> <CENTER> <font color="#000080">#</font></CENTER> </TD> <TD ALIGN = "Left" > <font color="#000080">Name</font> </TD> <TD ALIGN = "Right" > <CENTER> <font color="#000080">No</font></CENTER> </TD> <TD ALIGN = "Left" > <font color="#000080">Password</font> </TD> <TD ALIGN = "Center"> <font color="#000080">ID #</font> </TD> </TR> |
The HTML code above creates the following Table Title Bar in the first row of the table.
![]()
<?PHP
While (!$RecordSet->EOF)
{
?>
|
If the query is successful, the record set will contain information returned from the server. The record set pointer initially points to the first record in the record set. This record set point can be moved through the record set.
We would like to display the information from the current record in a row of our table and then move this record set pointer to the next record. This process shall continue in a While loop until the end of file is reached.
The basic form for the PHP While
Loop is :
While [Condition]
{
...........
}
<!-- ================================================================ --> <!-- One Row Per Record --> <!-- ================================================================ --> <TR> <TD ALIGN = "Center"> <?PHP Print $Counter; ?> </TD> <TD ALIGN = "Left"> <?PHP Print $Name->Value; ?> </TD> <TD ALIGN = "Center"> <?PHP Print $No->Value; ?> </TD> <TD ALIGN = "Left"> <?PHP Print $Password->Value; ?> </TD> <TD ALIGN = "Center"> <?PHP Print $ID->Value; ?> </TD> </TR> |
All of the code from the block above dumps output into a single row of the HTML table. A Counter is displayed in the first column. The current record's Name is placed in the second column. The current record's No is placed in the third column. The current record's Password is placed in the fourth column. The current record's ID is placed in the fifth column.
<?PHP
$Counter = $Counter + 1;
$RecordSet->MoveNext();
}
?>
|
Counter is simply a counter to
number the records; it is incremented each pass through the loop.
The record set pointer is moved to the next record.
Loop transfers control back to the While statement and repeats the block of
code within the loop untill the condition is no longer true. This loop
terminates when the record set pointer reaches the end of file.
</TABLE> |
The HTML code above simply ends the table.
<?PHP #-------------------------------------------------------------------------- # Close & Terminate The Connections #-------------------------------------------------------------------------- $RecordSet->Close(); $Conn->Close(); $RecordSet = null; $Conn = null; } ?> |
The PHP code above closes the connection set and closes the connection thus freeing resources on the server.
</BODY></HTML> |
The HTML code above simply ends the body and the document.
The complete code may be found below. A working model may be found at
Although it is not the purpose of this tutorial to teach PHP or HTML, a example is necessary to verify that the connection works.
<?PHP
#=========================================================================
#=========================================================================
#==== DisplaySecurityUsers.php ====
#=========================================================================
#==== ====
#==== Purpose : Display all of the information about all of the ====
#==== Users in the Security database using a RealPath ====
#==== connection to the Security.mdb that is three ====
#==== levels up at C:\ ====
#==== ====
#==== Written By : Dr. Thomas E. Hicks Date: 08/1/2003 ====
#=========================================================================
#=========================================================================
#--------------------------------------------------------------------------
# Include The Database Utilities
#--------------------------------------------------------------------------
Include('../adodb/adodb.inc.php');
#--------------------------------------------------------------------------
# Declarations & Initializations
#--------------------------------------------------------------------------
$Counter = 1;
#--------------------------------------------------------------------------
# Access Database RealPath Connect To The Server
#--------------------------------------------------------------------------
#------------------------ Create A Connection Object ----------------------
$Conn = New COM("ADODB.Connection");
#------------------------------ Absolute Path -----------------------------
$ConnStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" .
RealPath("C:\Security.mdb");
#------------------------------ Relative Path -----------------------------
$ConnStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" .
RealPath("../../../Security.mdb");
s
#---------------------------------- Connect -------------------------------
$Conn->Open($ConnStr);
#--------------------------------------------------------------------------
# Read All Of The User Names Into The RecordSet
#--------------------------------------------------------------------------
#--------------------------- Traditional SQL Query ------------------------
$UserSQL = ' SELECT * ' .
' FROM Users';
#------------- Create A Record Set That Contains Results Of Query ---------
$RecordSet = $Conn->Execute($UserSQL);
#------- If No Record Set Was Generated, Print Out The Error Message ------
If (!$RecordSet)
{
Print "---------------- Unable To Create Record Set ---------------";
Print $Conn->ErrorMsg();
}
Else
#------- If There Are No Records In The Record Set, Display Message -------
If ($RecordSet->EOF)
Print "----- There Are No Records That Match This Query -----";
#------------------ There Are Records To Display --------------------------
Else
{
$Name = $RecordSet->Fields(0);
$No = $RecordSet->Fields(1);
$Password = $RecordSet->Fields(2);
$ID = $RecordSet->Fields(3);
?>
<HTML><HEAD>
<TITLE>Dr. Thomas E. Hicks - DisplaySecurityUsers.php </TITLE>
</HEAD>
<BODY TEXT = "#000000"
BGCOLOR = "#000000"
VLINK ="#000000"
ALINK ="#000000"
BACKGROUND ="Paper.jpg">
<CENTER>
<FONT FACE="Arial" SIZE="4"> DisplaySecurityUsers.php <BR>
Written By<BR>
Dr. Thomas E. Hicks</font><P>
</CENTER>
<HR>
<!-- ================================================================ -->
<!-- Display Users In The Table -->
<!-- ================================================================ -->
<font face="System" size="3" color="#FFFFFF">
<TABLE BORDER = "5"
CELLPADDING = "4"
CELLSPACING = "4"
STYLE = "border-collapse: collapse"
BORDERCOLOR = "#800000"
BGCOLOR = "#FFFFFF"
WIDTH = "100%"
ID = "UserTable">
<!-- ================================================================ -->
<!-- Title Bar Row -->
<!-- ================================================================ -->
<TR><TD ALIGN = "Right">
<CENTER> <font color="#000080">#</font></CENTER> </TD>
<TD ALIGN = "Left" > <font color="#000080">Name</font> </TD>
<TD ALIGN = "Right" >
<CENTER> <font color="#000080">No</font></CENTER> </TD>
<TD ALIGN = "Left" > <font color="#000080">Password</font> </TD>
<TD ALIGN = "Center"> <font color="#000080">ID #</font> </TD>
</TR>
<?PHP
While (!$RecordSet->EOF)
{
?>
<!-- ================================================================ -->
<!-- One Row Per Record -->
<!-- ================================================================ -->
<TR>
<TD ALIGN = "Center"> <?PHP Print $Counter; ?> </TD>
<TD ALIGN = "Left"> <?PHP Print $Name->Value; ?> </TD>
<TD ALIGN = "Center"> <?PHP Print $No->Value; ?> </TD>
<TD ALIGN = "Left"> <?PHP Print $Password->Value; ?> </TD>
<TD ALIGN = "Center"> <?PHP Print $ID->Value; ?> </TD>
</TR>
<?PHP
$Counter = $Counter + 1;
$RecordSet->MoveNext();
}
?>
</TABLE>
<?PHP
#--------------------------------------------------------------------------
# Close & Terminate The Connections
#--------------------------------------------------------------------------
$RecordSet->Close();
$Conn->Close();
$RecordSet = null;
$Conn = null;
}
?>
</BODY></HTML>
|
You can see the results below:
