0% found this document useful (0 votes)
15 views

Web Programming ct214H Lab3 php1

The document discusses creating a MySQL database and PHP web pages to display song reviews from the database. It includes exercises to create tables in the database, query data, and design web pages to search and display reviews.

Uploaded by

tamb2111949
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Web Programming ct214H Lab3 php1

The document discusses creating a MySQL database and PHP web pages to display song reviews from the database. It includes exercises to create tables in the database, query data, and design web pages to search and display reviews.

Uploaded by

tamb2111949
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

WEB PROGRAMMING FUNDAMENTALS

LAB 3 – MySQL and PHP (Part 1)


Content:
- XAMM introduction: supported services, directory structure,…
- PHP - MySQL basics: create connection, create a database, manipulate data,
execute queries,…

Ex 1. Introduction to XAMPP, MySQL, and Apache.


v Objective: control MySQL and Apache, and upload a website into Apache.
v Requirements:
1) Run XAMPP and start Apache and MySQL
2) Check the working of MySQL
3) Check the working of Apache.

v Hints:
1) Start XAMPP (find its icon from the desktop or Start menu):

Click on the Start buttons to start MySQL and Apache services:

2) Check if the Apache is working: open the browser and go to http://localhost

Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -30-


3) Check if MySQL is working:
o Open the Command Prompt window
o Run the command (type the command and press Enter): mysql
o If it returns “Command not found”, check whether the current folder
is the MySQL folder. If not, change the current folder and rerun the
command.
o If the command returns some MySQL information, as shown in the
following figure, MySQL works properly.

o After connecting to MySQL, you can run some other commands to


make further checks: select version(); select user();
show databases;… (these commands are explained in Ch5 –
MySQL).

Ex 2. Create DB in MySQL
v Objective: Using DDL to create DB in MySQL

v Requirements: create a DB to store song reviews:


1) Create a MUSIC database.
2) Create the following tables in the MUSIC database:

Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -31-


Reviewer (tacgia):
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| ma_tgia | int(10) unsigned | NO | PRI | NULL | |
| ten_tgia | varchar(100) | NO | | NULL | |
| hinh | longblob | YES | | NULL | |
+----------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Genres
mysql> (theloai):
+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| ma_tloai | int(10) unsigned | NO | PRI | NULL | |
| ten_tloai | varchar(30) | NO | | NULL | |
+-----------+------------------+------+-----+---------+-------+
2 rows in set (0.37 sec)

Reviews
mysql> (baiviet):
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| ma_bviet | int(10) unsigned | NO | PRI | NULL | |
| tieude | varchar(200) | NO | | NULL | |
| ten_bhat | varchar(100) | NO | | NULL | |
| ma_tloai | int(10) unsigned | NO | | NULL | |
| tomtat | varchar(500) | NO | | NULL | |
| baiviet | varchar(2000) | YES | | NULL | |
| ma_tgia | int(10) unsigned | NO | | NULL | |
| ngayviet | date | NO | | NULL | |
| image | longblob | YES | | NULL | |
+----------+------------------+------+-----+---------+-------+
9 rows in set (0.04 sec)

3) Add data to the above tables using the following data:


mysql>

• Reviewer: http://bit.ly/OQXOcN (tacgia.txt)


• Genres: http://bit.ly/1nXaZJ9 (theloai.txt)
• Reviews: http://bit.ly/1rtY8gE (baiviet.txt)

v Hints:
1) Connect to MySQL using the root account, and create a database using the
CREATE DATABASE command.
2) Create tables:
• Choose the database MUSIC (use the USE … command)
• Create tables using the CREATE TABLE command.
3) Add data to the tables:
a. Download the supported files and write the INSERT INTO command
with the downloaded data
b. Save the command to a file with a .sql extension.
Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -32-
c. Use the command: source <tên tập tin .sql> t execute all
INSERT INTO in the sql file to add data to the tables.
d. Check the result using the SELECT * FROM … command.

Ex 3. Query data in the MUSIC database


v Objectives: Review SQL language.

v Requirements: Write SQL query to:


1) Select all “Nhạc trữ tình” songs.
2) Select all reviews of “Nhacvietplus”.
3) Select all reviews with the following information: review id, review title, song
name, reviewer name, song category, and review date.
4) Select the genre that has the max number of reviews
5) Select two songwriters who received the most reviews
6) Select all songwriters who wrote songs for at least two categories
7) Select all reviewers who reviewed for at least two genres
8) Select all reviews for songs whose names contain at least one of the following
words: “yêu”, “thương” “anh”, “em”.
9) Select all reviews whose name or the reviewed song name contains at least
one of the following words: “yêu”, “thương”, “anh”, “em”.

v Hints: Exercises 8) and 9)


We can use the LIKE operator for these queries. However, it is recommended to
use the fulltext search for these queries. To use fulltext search, we first create
a fulltext index for the table baiviet and use MATCH() … AGAINST() functions
in the queries.
Ex 4. Design a webpage to display the song reviews.
v Objectives:
1) Create a connection to MySQL
2) Query data in MySQL from PHP
3) Process the data returned from a query and display it on the webpage.

v Requirements: Design a webpage as described in Figure 20.


1) Displays all reviews
2) Display reviewer names and genre names instead of id.
3) Display a summary of the reviews (the first 50 words).

Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -33-


v Hints:
1) We must join 3 tables baiviet, tacgia và theloai to have all the necessary
information.
2) To display only 50 words of a review (summary), we can use the LEFT
function in the SELECT statement, or we SELECT the whole review text and
cut the first 50 words using the mb_str() function in PHP. This function
syntax is as follows:
mb_substr(string, start, number of chars, “UTF-8”)
3) Note: To work with the UTF-8 charset (Unicode), we need to call
mysql_set_charset(‘utf-8’) before executing the query.
4) We should put the information in appropriate <div> or <span> tags so that
we can later format the information easier.
5) In Figure 20, each review is placed in a <div> tag formatted similarly to a
<pre> tag.

Figure 20. Song reviews

Ex 5. Design a song review search page.


v Objective: create forms and send data to a web server for search data.

v Requirement:

Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -34-


1) Design a song review search as shown in Figure 21 and Figure 22.
2) The search result will be displayed as in Ex 4.
Note: the search keywords must be displayed in the search result page's
search box (see Figure 22).

Figure 21. Song review search page

Figure 22. Search result page

v Hint: see the example in the lecture.

Ex 6. (Extra) Improve the Ex 5 to allow users use the double quote in the search
keyword, e.g. “lòng mẹ”, “yêu thương”,…

Ex 7. (Extra) Improve Ex 4 so the review summary doesn’t break the last word. If the
50th character is not a space, move to the previous or next space to cut the summary
(see Figure 23 and compare to Figure 22 to figure out the requirement).
Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -35-
Figure 23. List of song reviews - Advanced

Web Programming Fundamentals Labs (CT214H) – TS. Trần Công Án -36-

You might also like