DataBases and Internet – Exercises

David Billard – University of Geneva, Switzerland

 

Thema: Using the MySQL Database Management System, the Apache Web Server and the PHP language to build a (very) basic web site.

Note: for your convenience, the CEENet staff has already downloaded and compiled all the necessary software. If you want to repeat these exercises in your country, you will have to install the software before beginning the exercises, and to adapt the exercises to your own configuration.

    1. Using MySQL
    2. We will use the MySQL Database Management System (DBMS) (http://www.mysql.com/). This DBMS is free of use under the Linux operating system. It is not very powerful, compared to Oracle, DB2 (IBM) or even PostgreSQL, but it is simple to install and administrate.

      1. Running the MySQL server
      2. Open a shell as user root, password ceenet.

        Go in the server/mysqld directory.

        Launch the server: ./mysqld start

      3. Playing with MySQL
      4. Go in the bin directory.

        Execute the MySQL classical client: ./mysql –u root

        The –u flag is to specify the user.

        [is_serv2@test1 bin]$ ./mysql -u root

        Welcome to the MySQL monitor. Commands end with ; or \g.

        Your MySQL connection id is 25 to server version: 3.22.32

        Type 'help' for help.

        mysql>

        Indicates that you want to use the administration database, whose name is always mysql: use mysql;

        Note that each SQL statement has a ; at the end.

        Print at the screen the names of the tables in the database mysql: show tables;

        mysql> show tables;

        +-----------------+

        | Tables in mysql |

        +-----------------+

        | columns_priv |

        | db |

        | func |

        | host |

        | tables_priv |

        | user |

        +-----------------+

        6 rows in set (0.00 sec)

        Print at the screen the content of the user table: select * from user;

         

        Print at the screen the content of the attributes host, user and password of the user table.

        mysql> select host, user, password from user;

        +-----------+-------+------------------+

        | host | user | password |

        +-----------+-------+------------------+

        | localhost | root | 4fdb530c48d73826 |

        | pc01 | root | |

        | localhost | | |

        | pc01 | | |

        | localhost | admin | 6f7064001f2be3a2 |

        +-----------+-------+------------------+

        5 rows in set (0.00 sec)

      5. Create a test database

To help you understand a bit of SQL, you must create a very simple database, composed of two tables:

id is a numeric identifier, auto-incremented, and is the primary key of the tables.

name, email, description and type are strings (varchar).

price is a real number (float).

name will be indexed and is considered as unique.

We first create the database: create database test1;

Then we create the Instructor table:

CREATE TABLE instructor (

id SMALLINT not null AUTO_INCREMENT,

name VARCHAR (50) not null,

email VARCHAR (50) not null,

birthdate DATE not null,

PRIMARY KEY (id),

INDEX (name),

UNIQUE (name));

Then we create the Lecture table:

CREATE TABLE Lecture (id SMALLINT not null AUTO_INCREMENT,

description VARCHAR (50) not null,

type CHAR DEFAULT 'R' not null,

price FLOAT DEFAULT '0' not null ,

id_instructor SMALLINT,

PRIMARY KEY (id));

Then we record an instructor:

INSERT INTO instructor (id, name, email, birthdate) VALUES ('', 'David Billard', 'David.Billard@adm.unige.ch', NOW(''));

Then we record a lecture:

INSERT INTO Lecture (id, description, type, price, id_instructor) VALUES ('', 'Electronic Commerce', '', '10.5', '1');

Then we print at the screen the content of the tables (you already know how to do it).

Then we want to know the name and email of the instructor of the lecture on Electronic Commerce:

select * from Lecture, instructor where Lecture.id_instructor=instructor.id;

Oops, just the name and email please.

As you can see, it is very tiring to do a single request by hand.

    1. Configuring the phpMyAdmin package
    2. Go to the /server/httpd/htdocs directory (or use a directory usable by the web server) and copy, gunzip and untar the package server/archive/phpMyAdmin-2.2.0rc3-php.tar.gz in this directory.

      Use Netscape and connect to the /server/httpd/htdocs/phpMyAdmin-2.2.0rc3-php/index.php page of your web server. You should have something like that:

       

      If you click on the “test1” link at the left of the window, your database structure will appear.

      You can then browse your tables, add records, etc…

    3. Exercice 1

Create a new database called shop.

Create the following set of tables and populate them at your whish:

Create a new directory under /server/httpd/htdocs, let say ceenet2001.

Create the file lpro.php that lists the products of the database.

Create the file lcli.php that lists the clients, and the file lord.php that lists the orders.

In case of trouble creating the files, there is an example in the appendix.

    1. Exercise 2

Modify or create the necessary files so that the Internet user is able:

    1. Notation
    2. Each step is rewarded by 1 point. If you fulfill all the steps you will have 4 points (and 5 points if you manage to send the email of exercise 2). The notation will be ECTS: 5 gives a A (Excellent), 4 a B (Very good) … 1 a E (just enough) and 0 a F (Failed)

    3. Appendix
    4. <?php

      $link = mysql_connect("computer_name", "user_name", "password")

      or die ("Could not connect");

      print ("Connected successfully<p>");

      mysql_select_db ("shop")

      or die ("Could not select database");

      print ("Database selected successfully<p>");

      ?>

      <h1>List of products</h1>

      <?php

      $result = mysql_query ("select description, price from products")

      or die ("Invalid query");

      $num_of_rows = mysql_num_rows ($result)

      or die ("The query did not return any data");

      print "The query returned $num_of_rows rows of data.<p>";

      for ($count = 1; $row = mysql_fetch_row ($result); ++$count) {

      print ("description of product $count = '$row[0]', price = $row[1] USD<br>");

      }

      ?>

      <h1>List of clients</h1>

      <?php

      $result = mysql_query ("select name, email from clients")

      or die ("Invalid query");

      for ($count = 1; $row = mysql_fetch_row ($result); ++$count) {

      print ("Name of client $count = '$row[0]', email = '$row[1]'<br>");

      }

      ?>

      <h1>Insert an order</h1>

      <?php

      $result = mysql_query ("insert into orders(id_client,id_product,quantity) values('1','1','3')")

      or die ("Invalid query");

      $result = mysql_query ("select id,id_client,id_product,quantity,timestamp from orders")

      or die ("Invalid query");

      for ($count = 1; $row = mysql_fetch_row ($result); ++$count) {

      print ("'$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]'<br>");

      }

      ?>

      <h1>Update an order</h1>

      <?php

      $result = mysql_query ("update orders SET quantity='10' where id='1'")

      or die ("Invalid query");

      $result = mysql_query ("select id,id_client,id_product,quantity,timestamp from orders")

      or die ("Invalid query");

      for ($count = 1; $row = mysql_fetch_row ($result); ++$count) {

      print ("'$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]'<br>");

      }

      mysql_close($link);

      ?>

    5. Database shop
# phpMyAdmin MySQL-Dump
# http://phpwizard.net/phpMyAdmin/
#
# Host: linux-billard Database : shop

# --------------------------------------------------------
#
# Table structure for table 'clients'
#

CREATE TABLE clients (
   id int(11) NOT NULL auto_increment,
   name varchar(50),
   email varchar(50) NOT NULL,
   PRIMARY KEY (id)
);

#
# Dumping data for table 'clients'
#

INSERT INTO clients VALUES( '1', 'David Billard', 'David.Billard@adm.unige.ch');
INSERT INTO clients VALUES( '2', 'Miroslav Milinovic', 'miroslav.milinovic@srce.hr');
INSERT INTO clients VALUES( '3', 'Sebastien Blondeel', 'blondeel@clipper.ens.fr');

# --------------------------------------------------------
#
# Table structure for table 'orders'
#

CREATE TABLE orders (
   id int(11) NOT NULL auto_increment,
   id_product int(11) NOT NULL,
   id_client int(11) NOT NULL,
   quantity int(11) NOT NULL,
   timestamp timestamp(14),
   PRIMARY KEY (id),
   KEY id (id, id_product, id_client)
);

#
# Dumping data for table 'orders'
#

INSERT INTO orders VALUES( '1', '1', '1', '10', '20010823133237');
INSERT INTO orders VALUES( '2', '1', '1', '10', '20010823133327');

# --------------------------------------------------------
#
# Table structure for table 'products'
#

CREATE TABLE products (
   id int(11) NOT NULL auto_increment,
   description varchar(50) NOT NULL,
   price float DEFAULT '0           ' NOT NULL,
   PRIMARY KEY (id)
);

#
# Dumping data for table 'products'
#

INSERT INTO products VALUES( '1', 'lecture DB in the Internet', '10          ');
INSERT INTO products VALUES( '2', 'Lecture Electronic Commerce', '50          ');
INSERT INTO products VALUES( '3', 'Exercises in DB', '5           ');
INSERT INTO products VALUES( '4', 'Exercises in EC', '8           ');