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

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Advertisements

Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 03 – Database Design: Multiple Tables.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 SOFT – Module Introduction.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Programming with php By: Seth Larson. A little bit about PHP  PHP stands for PHP:  Hypertext Preprocessor  PHP is a widely-used general-purpose server-side.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Mark Dixon 1 20 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 5 – Persistent data storage: relational databases.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Lecture 7 Interaction. Topics Implementing data flows An internet solution Transactions in MySQL 4-tier systems – business rule/presentation separation.
Concepts of Database Management Seventh Edition
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
M Dixon 1 Soft – Module Introduction and HTML.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
PHP meets MySQL.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Mark Dixon Page 1 18 – Web applications: Server-side code (PhP)
Mark Dixon 1 05 – JSP Databases: Multiple Tables.
Mark Dixon 1 06 – JSP Databases: Multiple Tables.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Concepts of Database Management Seventh Edition
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
Creating PHPs to Insert, Update, and Delete Data CS 320.
Mark Dixon Page 1 Soft – Module Introduction and HTML.
Database Management Systems.  Database management system (DBMS)  Store large collections of data  Organize the data  Becomes a data storage system.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Mark Dixon 1 21 – Databases: Multiple Tables. Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data.
When I want to work with SQL, I start off as if I am doing a regular query.
M Dixon 1 Tech HTML. M Dixon 2 Admin Attendance Register: –log in to your profile.
Mark Dixon Page 1 Soft – HTML. Mark Dixon Page 2 Admin Attendance Register: –log in to your profile.
Mark Dixon Page 1 Tech – HTML. Mark Dixon Page 2 Admin Attendance Register: –log in to your profile.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Web applications: Databases & ASP.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CSC 405: Web Application Engineering II Course Preliminaries Course Objectives Course Objectives Students’ Learning Outcomes Students’ Learning Outcomes.
MySQL MySQL and PHP – interacting with a database.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
Mark Dixon 1 07 – In-class test. Mark Dixon 2 In-class Test Date: Monday 5 th November 2012 Time: 11:05-120:55 Location: SMB109 Type: Individual 40% of.
Mark Dixon, SoCCE SOFT 131Page 1 14 – Databases: Multiple Tables.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
18 – Databases: Structured Query Language
ISC440: Web Programming 2 Server-side Scripting PHP 3
Server-Side Processing II
17 – Persistent data storage: relational databases and ADO
19 – Databases: Multiple Tables
Presentation transcript:

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

Mark Dixon Page 3 Admin: SQL Book Gennick J (2006) SQL Pocket Guide (2 nd edition). O'Reilly. ISBN:

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

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)

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

Mark Dixon Page 7 Record Field Example: People (Database) IDSurnameForenamesPhoneGender 1DixonMark Male 2SmithJohn Male 3JonesSally Female Information organised into –tables (e.g. person) –fields (e.g. phone) –records (e.g. 1 Dixon Mark …) Person

Mark Dixon Page 8 How many fields? How many records? Questions: Music (Database) Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track 9 3

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

Mark Dixon Page 10 PhpMyAdmin used to create tables, fields, etc. add port number :8080

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 ']. ' '; } mysql_free_result($q); ?> <?php echo $s; ?> Using Queries Connect to MySQL Select Database Get Data from Table Read Record / Row Read Field

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']. ' '; } mysql_free_result($q); echo $s;

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']. ' '; } mysql_free_result($q); echo $s; $s

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']. ' '; } mysql_free_result($q); echo $s; $s

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon Smith Dixon

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon Smith

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon Smith

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon Smith Jones Dixon Smith

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon Smith Jones

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']. ' '; } mysql_free_result($q); echo $s; $s Dixon Smith Jones

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']. ' '; } mysql_free_result($q); echo $s; Dixon Smith Jones $s

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']. ' '; } mysql_free_result($q); echo $s; Dixon Smith Jones $s

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 hand put br tags between data

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 = ; while ($r = mysql_fetch_array($q)){ $s = $s. $r['Name']; } mysql_free_result($q); Try putting br tag here php does not understand html

Mark Dixon Page 28 $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 double quotes around tag (php sees html as literal string) Countries.php Example: Countries runs, but br in wrong place

Mark Dixon Page 29 Countries.php Example: Countries Move br tag inside loop. Which bit of code pulls data from database? $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);

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']. " " ; } mysql_free_result($q); Countries.aspx Example: Countries Move br tag inside loop. after field data View Source

Mark Dixon Page 31 Embedding html in php html must be string (inside double quotes) follows normal pattern for expressions: data operator data operator $s = $s. " ". $r['Name']. " ";

Mark Dixon Page 32 Embedding html in php (errors) $s = $s. " " $r['Gender']. " "; missing operator $s = $s. $r['Height']. "; missing double quote $s = $s.. $r['Height']; html tag must be inside double quotes $s = $s. " ". $r[' Height']; looks for field in database called Height

Mark Dixon Page 33 Questions: HTML in php Are these correct (assume variables and fields exist)? $g = $g. $r['Surname ']; $h = $h. " " $r['Width']; $a = " ". $a. " "; $html = $html. " "; $h =. $h. " ";   

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']. ' '; } mysql_free_result($q); ?> Display Surname of Male people:

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']. ' '; } mysql_free_result($q); ?> SQL statement

Mark Dixon Page 36 SQL: Queries main purpose of databases: –get information back out: searching Structured Query Language –dedicated to interacting with databases 3 rd Generation Language (such as VB, C++) –code describes how to do task 4 th Generation Language (such as SQL) –code describes what to do (not how to do it)

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

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;

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'

Mark Dixon Page 40 Questions: SQL Create an SQL statement to extract Track Title of records by Aerosmith TrackTitleArtistNameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track SELECT TrackTitle FROM Track WHERE ArtistName = 'Aerosmith';

Mark Dixon Page 41 Questions: SQL Create an SQL statement to extract all fields of songs by Disturbed, ordered by track name TrackTitleArtistNameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track SELECT * FROM Track WHERE ArtistName = 'Disturbed' ORDER BY TrackTitle;

Mark Dixon Page 42 Example: People v4 User controls what is displayed:

Mark Dixon Page 43 SQL: DISTINCT records SELECT ArtistName FROM Track; ArtistName Black Sabbath Aerosmith Alien Ant Farm Disturbed ArtistName Black Sabbath Aerosmith Alien Ant Farm Disturbed SELECT DISTINCT ArtistName FROM Track;

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 address (You may want to use a Query String)