CS2008: Data Management Practical 8

In this practical you will learn writing PHP scripts for connecting to MySQL Server and perform database programming as described in the lectures. By now you should have an account on the MYSQL server, created in practical 3. If you don’t have one, go back to practical 3 to create the account. User manual for the MySQL server is available at http://dev.mysql.com/doc/mysql/en/index.html. PHP documentation on MySQL functions is available at http://uk2.php.net/manual/en/ref.mysql.php. From the CS2008 practicals page download p8.zip which has scripts you will use in this practical. There are three parts to this practical. In the first part you learn how to publish web pages on the University Web server. In the second part you learn how to create MySQL tables and insert data into them in the batch mode. Finally you learn to write PHP scripts that generate dynamic web pages on the University web server.

1. You can skip this part if you have already published web pages using University web server. If you are new to publishing web pages using the University server, follow the procedure described on http://www.abdn.ac.uk/webpack/factsheet10.shtml. You may find it useful to write a simple html/php script to test the procedure described in factsheet10. One such sample script, first.php is included in p8.zip. Do not proceed to the rest of the practical without learning the procedure described in factsheet10.

2. In this part you create DreamHome tables in your database on the MySQL server and load data into them. There are several ways to achieve this depending on the kind of interface available to you for accessing the MySQL server. In practical 3, you have used the web interface through PhpMyAdmin at http://www.abdn.ac.uk/local/mysql/phpmyadmin2/ to create DreamHome tables. Although it is easier to use the PhpMyAdmin interface to the MySQL server, in this practical you will use the programming language interface for interacting with your MySQL database.  Open the URL http://www.abdn.ac.uk/local/mysql/phpmyadmin2/. In the login dialog box, enter your MYSQL username and password (not your central systems one that you use to access your e-mail). Select your database created in practical 3. Drop all the existing DreamHome tables because you will load them now using a PHP script.  Copy dbsetup.php from p8.zip into your public.htm folder. Use any simple text editor such as WordPad to edit dbsetup.php to set the values for the variables $username and $password. For this simply replace username and password with your new MySQL account name and password in the mysql_connect function. In the function mysql_select_db replace ‘your database name’ with your database name.  Copy DreamHome.sql from p8.zip into your public.htm folder. This is an ascii file with all the SQL statements for creating DreamHome tables and inserting data into them. (Please inspect the contents of this file. You will find ‘Create Table’ and ‘Insert into’ SQL statements corresponding to each of the DreamHome tables.) You can use this to create a copy of the DreamHome database on the MySQL server. Copy dbRead.php from p8.zip into your public.htm folder. Run dbRead.php using your web browser with the URL: http://www.abdn.ac.uk/~username/dbRead.php where username is your own University computer login username. Don't forget the ~ symbol before your username!  You may like to test if your database now has the DreamHome tables and data using the PhpMyAdmin interface. Open URL http://www.abdn.ac.uk/local/mysql/phpmyadmin2/. In the login dialog box, enter your MYSQL username and password. In the left frame select your database from the database listbox. You can see a list of tables from the DreamHome database you have uploaded. 3. In this part you will develop a simple web interface to your DreamHome database. Focus on learning how to connect to the MySQL server, to issue a query and to process the results returned. Building more sophisticated user interfaces is a skill you will learn in other courses.  Copy the file ShowTables.php from p8.zip into your public.htm folder. This file contains partial code for displaying the results of SELECT queries. Your are required to compete missing portions. First add code to run the query ($query). Next add code to compute the number of rows ($numOfRows) and columns ($numOfFields) from the results ($result) object returned after query execution. Test the script ShowTables.php with a few select queries.  Modify the file ShowTables.php to allow users to run UPDATE queries (in addition to the SELECT queries) on the DreamHome database. To achieve this you will add another submit button to your HTML form and call it SubmitUPDATE. You may name the variable associated with this button as ‘update’. In the PHP part of ShowTables.php you will test for $update (in addition to the existing test for $select) and write PHP script to execute the $query and print an appropriate message to the user. The completed version of ShowTables.php should offer functionality similar to ShowTables.java from practical 7.  Create another new PHP script Properties.php that allows users to type in a city name and retrieve the details of properties in that city from the PropertyForRent table. The completed version of Properties.php should achieve functionality similar to Properties.java from practical 7. You may like to reuse most of the code from ShowTables.php while developing Properties.php.

4. The Web interface to student-mysql server available at http://www.abdn.ac.uk/local/mysql/phpmyadmin2/ provides functionality similar to what you have developed in ShowTables.php (SQL tab after you select the database). Use this interface to now run SELECT and UPDATE queries on your DreamHome database. PhPMyAdmin also provides MSAccess like query editor (Query-By-Example) which allows users to compose an SQL query using several GUI elements (Query tab after you select the database). Those of you who are interested in PHP programming can modify your ShowTables.php to develop such an interface. Do you find Query-By-Example interface useful when you are good at writing SQL queries?