1. BSc 2 Mathematics Programme – 20-5555: Web Programming
20-5555: Web Programming - PHP Tutorial 4
DATABASES ON THE WEB
This sheet introduces some of the techniques for dealing with databases using the
web. There are numerous techniques (e.g. Perl, PHP, ASP, Java server pages,
Coldfusion) and technologies (e.g. ODBC, SQL Server, Oracle, MySQL) for this – but
most share a similar idea:
1. A remote user makes an HTTP request of the web server
2. If this request is to a ‘Back-End-Enabled’ page (i.e. a server-scripted page), and if
it requires data to be supplied from a database, then a connection to that
database has to be established.
3. Each database technology will have its own way of doing this – as you’ll see from
the examples that follow.
4. The request for data is formulated using SQL (Structured Query Language) and
submitted to the database.
5. The data returned are formatted by the server-script and returned to the user.
In our examples, we will use PHP for 2,4 and 5.
For the database, we will experiment with three techniques –
• direct access using COM (Component Object Model - a Windows technology),
• access through ODBC (Open DataBase Connectivity) and
• access via a database server (we will use MySQL).
We need a simple database to use, and since we used the example of an on-line
Guestbook in the previous module, we can start with that.
Create a new Access database, and make a new table, called ‘Guestbook’ with the
following fields:
Add some data to this table – just make up a few entries.
Now save this (in Office 2003 format please) using a suitable name – e.g.
TestDB.mdb – to a folder on your computer.
1. Using PHP and COM
The following PHP code will print some details of all records in the database:
1
2. Tutorial Sheet 1
<html>
<body>
<?php
//create the database connection
$conn = new COM("ADODB.Connection");
$dsn = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath("testdb.mdb");
$conn->Open($dsn);
//pull the data through SQL string
$rs = $conn->Execute("select * from Guestbook");
$fn = $rs->Fields(1);
$ln = $rs->Fields(2);
$city = $rs->Fields(4);
$country = $rs->Fields(5);
while (!$rs->EOF){
echo "First Name: " . $fn->value . "<br />";
echo "Last Name: ". $ln->value . "<br />";
echo "City: ". $city->value . "<br />";
echo "Country: ". $country->value . "<br />";
echo "<hr />";
$rs->movenext();
}
//close the ADO connections and release resources
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>
</body>
</html>
Note the important points:
• The creation of a new COM object ($conn)
• The entry of the database filename (include the path if it’s in a different folder)
• The SQL query (select * from Guestbook)
• The extraction of the results from the returned recordset ($rs)
• Each field is assigned as an object (e.g. $fn=$rs.Fields(1)) and the value property
used to return its value ($fn->value).
• All records are retrieved by looping through the recordset in the while loop,
moving the record ‘pointer’ every time with the movenext() method.
• Close the recordset and connection, and release memory by assigning each object
the null value.
Test this out.
Here are some other SQL queries that illustrate how to do other common tasks:
$query="insert into modules (Code, Title, TutorID) VALUES ('$Code', '$Title',
'$Tutor')"
$query="update modules set Code='$Code', Title='$Title', TutorID='$TutorID' where
ID=".$Num
$query="SELECT ID,Code,Title FROM modules order by Code"
$query="DELETE from Guestbook where ID = 66"
2
3. BSc 2 Mathematics Programme – 20-5555: Web Programming
$query="SELECT * FROM Units where Level='$level' order by School_Code"
$query="select * from units,staff where units.Tutor=staff.Initials order
by units.Level,units.School_Code"
Using these, try writing code to
• print out, for example, only those that have left a message
• sort the printout according to the persons’ name
• only print out the entries that are not in the UK.
These are only examples – you are welcome to make up some others!
Try writing a web form that allows you to edit, add and delete records in the
Guestbook.
3