Professional Documents
Culture Documents
Version 2.4
Contents
1. Introduction
2. Using PHP to generate WWW pages dynamically
3. Using WWW pages that create and manipulate databases
4. Using SQL in PHP scripts to manipulate MySQL databases
5. Using Microsoft Access with MySQL databases
6. Resources
Document code: Guide 106
Title: Accessing databases from the World Wide Web
Version: 2.4
Date: 04/03/2010
Produced by: University of Durham Information Technology Service
Conventions:
In this document, the following conventions are used:
A typewriter font is used for what you see on the screen.
A bold typewriter font is used to represent the actual characters you type at
the keyboard.
A slanted typewriter font is used for items such as filenames which you should
replace with particular instances.
A bold font is used to indicate named keys on the keyboard, for example,
Esc and Enter, represent the keys marked Esc and Enter, respectively.
A bold font is also used where a technical term or command name is used in
the text.
Where two keys are separated by a forward slash (as in Ctrl/B, for example),
press and hold down the first key (Ctrl), tap the second (B), and then release
the first key.
Contents
1 Introduction ...................................................................................................... 1
1.1 Accessing large amounts of data from the WWW ......................................... 1
1.2 Providing a Microsoft Access database on the WWW .................................. 1
1.3 The drawbacks of providing a .mdb file......................................................... 1
1.4 An alternative approach ................................................................................ 1
2 Using PHP to generate WWW pages dynamically.......................................... 2
2.1 What is PHP? ............................................................................................... 2
2.2 A simple example of a PHP script................................................................. 3
2.3 Using variables in PHP ................................................................................. 3
2.4 Using a for command to create loops ........................................................... 3
2.5 Using an HTML form to supply data ............................................................. 5
2.6 Using an if command to make decisions ....................................................... 6
2.7 Calling pre-defined functions ........................................................................ 7
2.8 Providing output in the form of tables ........................................................... 7
2.9 Using literal HTML and combining print commands .................................... 10
3 Using WWW pages that create and manipulate databases ......................... 11
3.1 Introduction ................................................................................................ 11
3.2 Choosing to use MySQL for storing databases ........................................... 11
3.3 Administering MySQL from the web ........................................................... 11
3.4 Creating a database ................................................................................... 12
3.5 Creating a table within a database.............................................................. 12
3.5.1 Supplying the type for a column........................................................... 13
3.5.2 Supplying other attributes for a new column ........................................ 13
3.6 Displaying the headings of the columns of a table ...................................... 14
3.7 Adding a new column ................................................................................. 14
3.8 Inserting a new row into a table .................................................................. 14
3.9 Displaying data in a table............................................................................ 14
3.10 Dropping a table ......................................................................................... 14
3.11 Dropping a database .................................................................................. 14
4 Using SQL in PHP scripts to manipulate MySQL databases ....................... 14
4.1 The basics of SQL ...................................................................................... 14
4.1.1 What is SQL? ...................................................................................... 14
4.1.2 Accessing MySQL directly ................................................................... 15
4.1.3 Creating, dropping and using databases ............................................. 15
4.1.4 Creating, altering and dropping tables ................................................. 15
4.1.5 Inserting a new row of data into a table ............................................... 16
4.1.6 Querying a database ........................................................................... 16
4.1.7 Removing rows from a database ......................................................... 16
4.2 Accessing a MySQL server from PHP ........................................................ 16
4.3 Providing a WWW page to query a table .................................................... 17
4.4 Providing a WWW page to insert a new row ............................................... 20
4.5 Providing a WWW page to delete a row ..................................................... 21
5 Using Microsoft Access with MySQL databases ......................................... 23
5.1 Introducing the two ways in which Access can be used .............................. 23
5.2 Installing a MySQL ODBC driver on a Windows PC ................................... 24
5.3 Transferring a table to a MySQL database from Access ............................. 24
5.3.1 Creating a database in Microsoft Access 2007 .................................... 24
5.3.2 Creating a database in Microsoft Access 2003 .................................... 25
5.3.3 Creating a MySQL database ............................................................... 26
Note: the line numbers (and the trailing colon) do not form part of the script.
They are just included to enable these notes to refer to specific lines of the
scripts.
Suppose that the file hello1.php contains the above script. It can be
executed by passing it to a WWW server that understands PHP. This can
be done by using a WWW browser with the URL
http://www.dur.ac.uk/resources/its/info/guides/106/hello1.php
Because the filename ends in .php, the WWW server will pass this to a
PHP interpreter which understands the bits inside the <?php and ?>
brackets. The print command of PHP just outputs the string that follows
print. The string ends with \n which means move to a new line (in the HTML
output). So the PHP interpreter will output the following HTML and this is
what the user’s browser sees:
4: <p>Hello Fred. I hope you are OK.</p>
When the PHP interpreter sees this, it will execute the assignment
command that stores the string "Fred" in the variable firstname and then it
will obey the print command. In the print command, it will replace the
$firstname by the value of the variable. So the result is the same piece of
HTML that was produced by the hello1.php script, i.e.:
9: <p>Hello Fred. I hope you are OK.</p>
The lines that are surrounded by a pair of curly brackets form the body of
the for command. It is these lines that are repeatedly executed. The other
part of a for command contains something like:
13: for ($paranum = 0; $paranum<4; $paranum++)
Although there are many possibilities for this part, most for commands
introduce a variable that is initialized to either 0 or 1 and is increased by 1
just before each re-execution of the body. How does the loop terminate?
Well, each time, just before executing the body, a condition is checked, and
the loop terminates if the condition is false.
So, in the above example, the $paranum = 0 is the bit that causes a variable
called paranum to be initialized to 0. The $paranum<4 is the condition, the bit
that checks, in this case, whether the variable paranum has a value less
than 4. And $paranum++ is a bit of shorthand that increases the value of
paranum by 1.
As mentioned earlier, the WWW server gets a PHP interpreter to
understand the text that is in a PHP script. After the PHP interpreter has
done its job, the WWW server sends the HTML that the PHP interpreter
has produced to the WWW browser. For this example, the browser would
see:
19: <p>Here is an introductory paragraph.</p>
20: <p>Hello Fred. I hope you are OK.</p>
21: <p>Hello Fred. I hope you are OK.</p>
22: <p>Hello Fred. I hope you are OK.</p>
23: <p>Hello Fred. I hope you are OK.</p>
24: <p>Here is a final paragraph.</p>
This script assumes that it is triggered into action by an HTML form similar
to the hello5.htm given above. A page with this form is at
http://www.dur.ac.uk/resources/its/info/guides/106/hello6.htm
Here is how this if command works. First, the condition $numparas<=0 is
evaluated; this is being used to check whether the value of numparas is less
than or equal to zero. If it is, then the command(s) inside:
59: {
60: print "<p>That is a silly value for the number of paragraphs!</p>\n";
61: }
get executed.
This example uses the format "Y-m-d". This particular format gives a string
like 2008-04-21. The call of the function appears on the right-hand side of
an assignment command, and the string that is produced is assigned to the
variable called today. You can execute the above script using
http://www.dur.ac.uk/resources/its/info/guides/106/today1.php
Here is one other useful bit of HTML which we will use when displaying
tables: if instead of <td> you use <td bgcolor="yellow"> you can arrange for
that cell to be output with a yellow background.
So, suppose we want to calculate and output a table giving the Centigrade
equivalent of the Fahrenheit temperatures in the range 175 to 525 at steps
of 25 degrees Fahrenheit, with the Centigrade values rounded to the
nearest 10 degrees. Such tables used to appear in cookery books.
As we need to calculate a Centigrade figure for each of the Fahrenheit
figures, it makes sense to use a for command which looks a bit like this:
foreach Fahrenheit figure
{
calculate the Centigrade figure
output the Fahrenheit figure
output the Centigrade figure
}
rather than:
115: print "<td bgcolor="yellow">";
The script uses a function called round. This function returns a value which
is the nearest integer to the value of its argument.
Besides generating the HTML instructions for the table, the script also
generates the instructions:
at the end. To save space, in the previous examples given in these notes,
we have failed to provide these instructions. Although most WWW
browsers will accept the minimal set of HTML instructions shown in earlier
examples, it is better style to provide the fuller set shown in this example.
3.1 Introduction
In Section 4 of these notes, we will look at how we can manipulate
databases from a PHP script triggered from an HTML form on a WWW
page. We will produce the files containing the HTML forms and PHP scripts
ourselves.
However, to give you some idea about what can be done, in this section of
the notes, we will create a database, add a table to the database, insert
some values into this table, remove a row of the table, and so on. We will
do this by accessing some WWW pages that have already been created:
they have HTML forms and PHP scripts that do this work for us.
where dxy3fab is replaced by your username. MySQL will then prompt you
for your password. Any SQL statements you type in here must finish with a
semicolon.
Here a table called consum is created: it has three columns, called ID, goods
and price. The first column can contain an integer value, the second can
contain strings up to 40 characters long, whereas the third can contain a
numerical value.
Later, you can alter the design of the table, e.g., by adding an extra column,
dropping a column, changing the type of a column, and so on. For example:
ALTER TABLE consum ADD COLUMN number_in_stock INT DEFAULT 0
Because we have not supplied a value for the number_in_stock column, this
row will have a number_in_stock value of 0, because an attribute for this
column (that was given earlier) said that the default value is 0.
would look at the table consum and produce (a new table containing) the
goods and prices from all the rows of the table consum. If you want the
values of all of the columns, use:
SELECT * FROM consum
would just deliver the names of those products whose price is less than 1.0.
Another example is:
SELECT * FROM consum WHERE goods='3.5 inch Floppy disk'
would deliver all the columns that have a goods column having the value
'3.5 inch Floppy disk'. If you wish to give the option of supplying only part of a
value (or you are uncertain as to what the column contains!), you can
instead use:
SELECT * FROM consum WHERE goods LIKE '%Floppy%'
where the % means that any characters may appear at this point.
And it is possible to use a SELECT statement that accesses more than one
table. Suppose the usernames table has columns labelled employeenumber,
surname, initials and username, and the phones table has columns labelled
employeenumber and phonenumber. You could then use:
SELECT surname, initials, username, phonenumber FROM usernames, phones
WHERE usernames.employeenumber=phones.employeenumber
mysql_num_rows which finds out how many rows there are in the result
of a query:
$numrows = mysql_num_rows($query_result);
i.e., a string containing the query in which the visitor is interested. It then
calls the function mysql_db_query to send this SELECT statement to the
database Pdcl0bjc_prices. If this is successful, the variable query_result
now contains a pointer to a temporary table that has just been created;
it contains only those rows satisfying the query.
The call of mysql_num_rows finds out how many rows are in this table. And if
this is not zero, the script generates an HTML table containing the rows of
data that are in this table. It does this by using a for command to wander
through each row of the table. For each row, it generates HTML containing
the contents of the ID, goods and price columns.
The above script generates an HTML form, and if a visitor presses either of
its two buttons, the following script (pricesreallydelete.php) is executed. It
detects which of the two buttons was pressed, and performs an appropriate
DELETE SQL statement if and only if the visitor pressed the delete item
button.
6 Resources
6.1 PHP
The manual for PHP is enormous, but it is very useful. Although the WWW
site for PHP is http://www.php.net/, you should get faster response from
the UK mirror site at http://uk.php.net/. The page about the documentation
that is available for PHP is at http://uk.php.net/docs.php. From this page,
you can see that you can get the documentation in many different ways.
For example, the manual mentioned above is presented using more
sophisticated HTML at http://uk.php.net/manual/.
6.2 SQL
The University Library has a number of books about SQL. Probably the
best is Introduction to SQL: Mastering the Relational Database Language
6.3 MySQL
Although the WWW site for MySQL is http://www.mysql.com/, you should
get faster response from one of the UK mirror sites at
http://www.mirrorservice.org/sites/ftp.mysql.com/.