mark dixon page 1 21 – persistent data storage: relational databases and mysql

44
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Upload: annice-johnson

Post on 01-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 1

21 – Persistent data storage: relational databases and MySQL

Page 2: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 2

Questions: Session variables• Write a line of code to put 59 into a session

variable called score.

• Write code that adds 1 to a variable called g, when a session variable called i is over 25.

if($_SESSION["i"] > 25){

$g = $g + 1

}

$_SESSION["score"] = 59

Page 3: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 3

Admin: SQL BookGennick J (2006) SQL Pocket Guide (2nd

edition). O'Reilly.ISBN: 0-596-52688-1

Page 4: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 4

Session Aims & Objectives• Aims

– To introduce the fundamental ideas involved in using relational databases for persistent data storage

• Objectives,by end of this week’s sessions, you should be able to:

– create a relational database table– create a web page that displays data from a

single table in a database– use SQL in your programs to create more

complex record-sets, using records and fields

Page 5: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 5

Persistent Data Storage• So far

– programs (web-pages) lose data when closed

• Not realistic– typically data stored to persistent storage device

(e.g. hard disk, key drive, floppy disk, CD-RW)

• Use either– flat files– database (relational, or object oriented)

Page 6: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 6

Example: People (analysis)SPECIFICATION

• User Requirements – need to have access to people's details

• Software Requirements– Functional:

–Display list of people from a database– Non-functional

should be viewable anywhere in the world

Page 7: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 7

Record Field

Example: People (Database)

ID Surname Forenames Phone Gender

1 Dixon Mark 01752 232556 Male

2 Smith John 01752 111111 Male

3 Jones Sally 01752 888888 Female

• Information organised into– tables (e.g. person)– fields (e.g. phone)– records (e.g. 1 Dixon Mark 01752 232556 …)

Person

Page 8: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 8

• How many fields?

• How many records?

Questions: Music (Database)

Track Title Artist Name Country

Paranoid Black Sabbath UK

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

9

3

Page 9: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 9

DBMS• Database Management Systems (DBMS)

provide facilities for:– creating and changing databases

• add/remove records• add/remove fields• add/remove data

– For example:• Microsoft Access• dBase• Borland Paradox• MySQL• Microsoft SQL Server• Oracle

home/small business

large scale

Page 10: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 10

PhpMyAdmin• used to create tables, fields, etc.

add port number:8080

Page 11: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 11

<?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);?>

<html> <head><title></title></head> <body> <?php echo $s; ?> </body></html>

Using QueriesConnect to MySQL

Select Database

Get Data from Table

Read Record / Row

Read Field

Page 12: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 12

People.php

Example: People (Open)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 13: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 13

People.php

Example: People$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$s

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 14: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 14

People.php

Example: People (EOF)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$s

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 15: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 15

People.php

Example: People$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 16: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 16

People.php

Example: People (Loop)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 17: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 17

People.php

Example: People (EOF)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 18: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 18

People.php

Example: People$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />Smith<br />Dixon<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 19: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 19

People.php

Example: People (Loop)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />Smith<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 20: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 20

People.php

Example: People (EOF)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />Smith<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 21: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 21

People.php

Example: People$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />Smith<br />Jones<br />Dixon<br />Smith<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 22: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 22

People.php

Example: People (Loop)$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />Smith<br />Jones<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 23: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 23

People.php

Example: People (EOF)rs

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

$sDixon<br />Smith<br />Jones<br />

ID Surname Forenames Phone gender1 Dixon Mark 01752 232556 Male2 Smith John 01752 111111 Male3 Jones Sally 01752 888888 Female

Page 24: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 24

People.php

Example: People$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

Dixon<br />Smith<br />Jones<br />

$s

Page 25: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 25

People.php

Example: People$q

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);

echo $s;

Dixon<br />Smith<br />Jones<br />

$s

Page 26: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 26

Countries.php

Example: Countries $c = mysql_connect('localhost', 'root', ''); mysql_select_db('Countries'); $q = mysql_query('SELECT * FROM Country;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Name']; } mysql_free_result($q);

Need data on separate lines

html by handput br tags between data <br />

Page 27: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 27

Countries.php

Example: Countries (error) $c = mysql_connect('localhost', 'root', ''); mysql_select_db('Countries'); $q = mysql_query('SELECT * FROM Country;'); mysql_close($c);

$s = <br />; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Name']; } mysql_free_result($q);

Try putting br tag here

php does not understand html

Page 28: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 28

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('Countries'); $q = mysql_query('SELECT * FROM Country;'); mysql_close($c);

$s = "<br />"; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Name']; } mysql_free_result($q);

• Need double quotes around tag

• (php sees html as literal string)

Countries.php

Example: Countries

runs, but br in wrong place

Page 29: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 29

Countries.php

Example: Countries

Move br tag inside loop.Which bit of code pulls datafrom database?

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('Countries'); $q = mysql_query('SELECT * FROM Country;'); mysql_close($c);

$s = "<br />"; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Name']; } mysql_free_result($q);

Page 30: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 30

$c = mysql_connect('localhost', 'root', ''); mysql_select_db('Countries'); $q = mysql_query('SELECT * FROM Country;'); mysql_close($c);

$s = ""; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Name'] . "<br />"; } mysql_free_result($q);

Countries.aspx

Example: Countries

Move br tag inside loop.after field data

View Source

Page 31: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 31

Embedding html in php• html must be string (inside double quotes)

• follows normal pattern for expressions:

data

operator

data data data

operator operator

$s = $s . "<b>" . $r['Name'] . "</b>";

Page 32: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 32

Embedding html in php (errors)

$s = $s . "<i>" $r['Gender'] . "</i>";

missing operator

$s = $s . $r['Height'] . </i>";

missing double quote

$s = $s . <ul> . $r['Height'];

html tag must be inside double quotes

$s = $s . "<ul>" . $r['<b>Height'];

looks for field in database called <b>Height

Page 33: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 33

Questions: HTML in php• Are these correct (assume variables and

fields exist)?

$g = $g . $r['Surname<br />'];

$h = $h . "<ol>" $r['Width'];

$a = "<p>" . $a . "</p>";

$html = $html . "<img src=face.gif />";

$h = <table> . $h . "</table>";

Page 34: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 34

Example: People v2

<?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ if($r['Gender'] == 1){ $s = $s . $r['Surname'] . '<br />'; } } mysql_free_result($q);?>

• Display Surname of Male people:

Page 35: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 35

Example: People v3• Display Surname of Male people:

<?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person WHERE Gender=1;'); mysql_close($c);

$s = ''; while ($r = mysql_fetch_array($q)){ $s = $s . $r['Surname'] . '<br />'; } mysql_free_result($q);?>

SQL statement

Page 36: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 36

SQL: Queries• main purpose of databases:

– get information back out: searching

• Structured Query Language– dedicated to interacting with databases

• 3rd Generation Language (such as VB, C++)– code describes how to do task

• 4th Generation Language (such as SQL)– code describes what to do (not how to do it)

Page 37: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 37

SQL: SELECT statement• SELECT statement

– used to get data– can be embedded in php, via mysql_query:

$q = mysql_query('SELECT * FROM Person');

all fields

Page 38: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 38

SQL: WHERE & ORDER BY• WHERE clause

– used to restrict data

SELECT * FROM People WHERE age>=18;

• ORDER BY clause– used to change order of data

SELECT * FROM People ORDER BY Surname;

Page 39: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 39

SQL: strings (text data)• Possible confusion:

SELECT * FROM Person WHERE Surname = Smith

this will look for field called Smith - gives error

• need single (SQL) quotes to signify literal text SELECT * FROM Person WHERE Surname = 'Smith'

Page 40: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 40

Questions: SQL• Create an SQL

statement to extract Track Title of records by Aerosmith

TrackTitle ArtistName Country

Paranoid Black Sabbath UK

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm

US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness

Disturbed US

Track

SELECT TrackTitle FROM Track WHERE ArtistName = 'Aerosmith';

Page 41: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 41

Questions: SQL• Create an SQL

statement to extract all fields of songs by Disturbed, ordered by track name

TrackTitle ArtistName Country

Paranoid Black Sabbath UK

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm

US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness

Disturbed US

Track

SELECT * FROM Track WHERE ArtistName = 'Disturbed' ORDER BY TrackTitle;

Page 42: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 42

Example: People v4

• User controls what is displayed:

Page 43: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 43

SQL: DISTINCT records

SELECT ArtistNameFROM Track;

ArtistName

Black Sabbath

Aerosmith

Aerosmith

Aerosmith

Alien Ant Farm

Disturbed

Disturbed

Disturbed

Disturbed

ArtistName

Black Sabbath

Aerosmith

Alien Ant Farm

Disturbed

SELECT DISTINCT ArtistNameFROM Track;

Page 44: Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL

Mark Dixon Page 44

Tutorial Exercise: People• Task 1: Create your own People database:

– Start WAMP Server– Open PhpMyAdmin– Create a new database– Create a new table– Create fields– Enter data

• Task 2: Get the People v1 example (from the lecture) working.• Task 3: Modify your page so that it displays phone number as well as the person's name.• Task 4: Modify your page so that it displays the data in an html table.• Task 5: Modify your page so that records for males are displayed in blue, and records for

females are displayed in red.• Task 6: Modify your page as per version 4. You will need to:

– Add a form to the page, and three submit buttons– In your code, detect when a button has been pressed (have a look at previous

weeks)• Task 7: Modify your page so that the user can type a letter, and only names starting with

that letter are displayed.• Task 8: Modify your page so that the user can type a series of numerical digits and only

phone numbers containing those digits are displayed.• Task 9: Modify your code so that the user can order the data by surname, or email

address (You may want to use a Query String)