Web
+ Database
Useful
Web Apps
CGS 2835

Interdisciplinary Web Development
The Value of Databases
• Databases and Database Management Systems (DBMS)
transform large quantities of data into specific...
AMP
Today’s Dominant Web Architecture

• Apache – open source Web server
• MySQL – open source database
• PHP – programmin...
AMP
Today’s Dominant Web Architecture

•
•
•
•
•

LAMP (Linux AMP)
SAMP (Solaris AMP)
WAMP (Windows AMP)
MAMP (Mac AMP)
XA...
http://iSpace.ci.fsu.edu/~username/mysite/index.html?name=geo

HTML REQUEST w/DATA
HTML REQUEST w/DATA

Web
Server

PHP
Da...
PHP-created HTML

Web
Server

PHP
Database
Server

CGS 2835

data

Interdisciplinary Web Development
Our Focus
PHP-created HTML
MySQL
Database
Server

CGS 2835

SQL
SQL

data

PHP

Interdisciplinary Web Development
Databases

CGS 2835

Interdisciplinary Web Development
Database

• A collection of data organized to meet user’s
needs.
File or Table
Field
(Attribute)

Records
(Entities)

CGS ...
Database Fields
• Fields are set to hold
specific types of data.

CGS 2835

Interdisciplinary Web Development
Database

A Database
is a
collection of
files/tables

CGS 2835

Interdisciplinary Web Development
Database Heirarchy

Table

CGS 2835

Interdisciplinary Web Development
Keys and Primary Key
• Key: A field in a record that is used to identify
the record
• Primary key: A field that uniquely i...
Primary Keys

Which field would act as the best
primary key?

CGS 2835

Interdisciplinary Web Development
Primary Key

CGS 2835

Interdisciplinary Web Development
The Relational Model
• In a relational database, tables are linked (related) through
common fields.

CGS 2835

Interdiscip...
Relation Types
• One-to-many

– Most typical
– Makes use of primary key

• One-to-one
• Many-to-many

CGS 2835

Interdisci...
MySQL
An open-source, relational database
ideal for use with PHP

CGS 2835

Interdisciplinary Web Development
MySQL - Installation
• MySQL is included with AMP
• and installed on iSpace
http://localhost

CGS 2835

Interdisciplinary ...
MySQL – Creating a Database
• For security reasons MySQL accounts are
created through MySQL Administrative Tools
• Creatin...
MySQL – Creating a Database
https://ispace-tools.cci.fsu.edu

CGS 2835

Interdisciplinary Web Development
You will need this
You will need this
information for
information for
use in your PHP
use in your PHP
Code
Code

Click Edi...
phpMyAdmin
is a popular application
for managing mySQL
databases. It’s provided
at tools.ci.fsu.edu and
also often include...
Access MySQL Database
We will use phpMyAdmin
to:

We will use PHP to:

•Create our databases
•Create tables in the databas...
When I click my database link in phpMyAdmin, I am prompted to create a table.

CGS 2835

Interdisciplinary Web Development
Then I create fields for my table.

CGS 2835

Interdisciplinary Web Development
I can then use the
Insert tab to enter
records into the table.

CGS 2835

Interdisciplinary Web Development
CGS 2835

Interdisciplinary Web Development
The browse buttons allows you to view the contents of the table.

CGS 2835

Interdisciplinary Web Development
CGS 2835

Interdisciplinary Web Development
You can also run SQL
queries on your data from
within phpMyAdmin.

CGS 2835

Interdisciplinary Web Development
MySQL & phpMyAdmin
• phpMyAdmin is a useful tool for manipulating mySQL
databases; however, in order to work with Web-gene...
MySQL & phpMyAdmin
• Either method of interacting with the database
requires knowledge of SQL
MySQL
Database
Server

SQL
S...
SQL – The Basics

CGS 2835

Interdisciplinary Web Development
Database Strengths
 The power of a database and DBMS lies in the
user’s ability to manipulate the data to turn up
useful ...
Data Manipulation Language
• A Data Manipulation Language (DML) is a specific
language provided with the DBMS that allows ...
SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a datab...
Employees Table

SELECT
SELECT field_names(s)
FROM table_name

Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Wil...
Employees Table

SELECT

Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith

SELECT column_name(s)
FRO...
SELECT

Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith

FirstName
Ola
Jon
Kari
Car...
UPDATE

Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith

FirstName
Ola
Jon
Kari
Car...
INSERT INTO

Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith

FirstName
Ola
Jon
Kar...
DELETE

Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith

FirstName
Ola
Jon
Kari
Car...
PHP > MySQL

CGS 2835

Interdisciplinary Web Development
Accessing a MySQL Database from PHP
First create a database, table, and fields using phpMyAdmin

1.Establish a connection ...
1. Establish a Connection
mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());

CGS 2835

I...
2. Select the Database
mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());

mysql_select_d...
3. Use mysql_query
to Issue Commands
mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());

...
3. Use mysql_query
to Issue Commands
mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());

...
Useful Resources
• Tizag PHP/MySQL Tutorial
– http://www.tizag.com/mysqlTutorial

• W3Schools
– PHP MySQL: http://www.w3sc...
Upcoming SlideShare
Loading in …5
×

Database

5,474 views

Published on

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
  • Excellent Doc.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,474
On SlideShare
0
From Embeds
0
Number of Embeds
1,279
Actions
Shares
0
Downloads
232
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Database

  1. 1. Web + Database Useful Web Apps CGS 2835 Interdisciplinary Web Development
  2. 2. The Value of Databases • Databases and Database Management Systems (DBMS) transform large quantities of data into specific and valuable information for accomplishing some goal. CGS 2835 Interdisciplinary Web Development
  3. 3. AMP Today’s Dominant Web Architecture • Apache – open source Web server • MySQL – open source database • PHP – programming language that works in HTML to provide interactivity with MySQL CGS 2835 Interdisciplinary Web Development
  4. 4. AMP Today’s Dominant Web Architecture • • • • • LAMP (Linux AMP) SAMP (Solaris AMP) WAMP (Windows AMP) MAMP (Mac AMP) XAMPP (Cross Platform AMP + Perl) CGS 2835 Interdisciplinary Web Development
  5. 5. http://iSpace.ci.fsu.edu/~username/mysite/index.html?name=geo HTML REQUEST w/DATA HTML REQUEST w/DATA Web Server PHP Database Server CGS 2835 SQL Command SQL Command Interdisciplinary Web Development
  6. 6. PHP-created HTML Web Server PHP Database Server CGS 2835 data Interdisciplinary Web Development
  7. 7. Our Focus PHP-created HTML MySQL Database Server CGS 2835 SQL SQL data PHP Interdisciplinary Web Development
  8. 8. Databases CGS 2835 Interdisciplinary Web Development
  9. 9. Database • A collection of data organized to meet user’s needs. File or Table Field (Attribute) Records (Entities) CGS 2835 Interdisciplinary Web Development
  10. 10. Database Fields • Fields are set to hold specific types of data. CGS 2835 Interdisciplinary Web Development
  11. 11. Database A Database is a collection of files/tables CGS 2835 Interdisciplinary Web Development
  12. 12. Database Heirarchy Table CGS 2835 Interdisciplinary Web Development
  13. 13. Keys and Primary Key • Key: A field in a record that is used to identify the record • Primary key: A field that uniquely identifies a record – A primary key field prevents duplicate records from occurring in a table. CGS 2835 Interdisciplinary Web Development
  14. 14. Primary Keys Which field would act as the best primary key? CGS 2835 Interdisciplinary Web Development
  15. 15. Primary Key CGS 2835 Interdisciplinary Web Development
  16. 16. The Relational Model • In a relational database, tables are linked (related) through common fields. CGS 2835 Interdisciplinary Web Development
  17. 17. Relation Types • One-to-many – Most typical – Makes use of primary key • One-to-one • Many-to-many CGS 2835 Interdisciplinary Web Development
  18. 18. MySQL An open-source, relational database ideal for use with PHP CGS 2835 Interdisciplinary Web Development
  19. 19. MySQL - Installation • MySQL is included with AMP • and installed on iSpace http://localhost CGS 2835 Interdisciplinary Web Development http://tools.ci.fsu.edu
  20. 20. MySQL – Creating a Database • For security reasons MySQL accounts are created through MySQL Administrative Tools • Creating a MySQL account is the only database command that cannot be issued from PHP code. CGS 2835 Interdisciplinary Web Development
  21. 21. MySQL – Creating a Database https://ispace-tools.cci.fsu.edu CGS 2835 Interdisciplinary Web Development
  22. 22. You will need this You will need this information for information for use in your PHP use in your PHP Code Code Click Edit to manage the database Click Edit to manage the database with phpMyAdmin with phpMyAdmin CGS 2835 Interdisciplinary Web Development
  23. 23. phpMyAdmin is a popular application for managing mySQL databases. It’s provided at tools.ci.fsu.edu and also often included with AMP. Here you can click a database name to access its properties and tools. You can even create new databases from here. Note that whenever prompted for charset or collation select UTF-8 Unicode (utf8). CGS 2835 Interdisciplinary Web Development
  24. 24. Access MySQL Database We will use phpMyAdmin to: We will use PHP to: •Create our databases •Create tables in the databases •Create fields in the tables •Manage the database •Examine table data • Enter records into tables from HTML forms • Read records from tables and output to HTML pages CGS 2835 Interdisciplinary Web Development
  25. 25. When I click my database link in phpMyAdmin, I am prompted to create a table. CGS 2835 Interdisciplinary Web Development
  26. 26. Then I create fields for my table. CGS 2835 Interdisciplinary Web Development
  27. 27. I can then use the Insert tab to enter records into the table. CGS 2835 Interdisciplinary Web Development
  28. 28. CGS 2835 Interdisciplinary Web Development
  29. 29. The browse buttons allows you to view the contents of the table. CGS 2835 Interdisciplinary Web Development
  30. 30. CGS 2835 Interdisciplinary Web Development
  31. 31. You can also run SQL queries on your data from within phpMyAdmin. CGS 2835 Interdisciplinary Web Development
  32. 32. MySQL & phpMyAdmin • phpMyAdmin is a useful tool for manipulating mySQL databases; however, in order to work with Web-generated data, the mySQL database must be accessed from the HTML code using PHP. MySQL Database Server SQL SQL CGS 2835 SQL SQL Interdisciplinary Web Development PHP
  33. 33. MySQL & phpMyAdmin • Either method of interacting with the database requires knowledge of SQL MySQL Database Server SQL SQL CGS 2835 SQL SQL Interdisciplinary Web Development PHP
  34. 34. SQL – The Basics CGS 2835 Interdisciplinary Web Development
  35. 35. Database Strengths  The power of a database and DBMS lies in the user’s ability to manipulate the data to turn up useful information. • Data can be sifted, sorted and queried through the use of data manipulation languages. CGS 2835 Interdisciplinary Web Development
  36. 36. Data Manipulation Language • A Data Manipulation Language (DML) is a specific language provided with the DBMS that allows people and other database users to access, modify, and make queries about data contained in the database, and to generate reports. • Structured Query Language (SQL): The most popular DML. – SELECT * FROM EMPLOYEE WHERE JOB_CLASSIFICATION = ‘C2” CGS 2835 Interdisciplinary Web Development
  37. 37. SQL Commands SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index From www.w3schools.com/sql CGS 2835 Interdisciplinary Web Development
  38. 38. Employees Table SELECT SELECT field_names(s) FROM table_name Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith FirstName Ola Jon Kari Carl Jason Address 120 Main St 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr Examples: SELECT LastName,FirstName FROM Employees LastName Baldauf Svendson Pettersen Willis Smith SELECT * FROM Employees Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith CGS 2835 FirstName Ola Jon Kari Carl Jason FirstName Ola Jon Kari Address 120 Main St 3 Bogus Dr 2413 Sayer Ave City Chicago Tallahassee Tallahassee Carl Jason 12 Bacon Cr Atlanta Interdisciplinary Web Development City Chicago Tallahassee Tallahassee Atlanta
  39. 39. Employees Table SELECT Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith SELECT column_name(s) FROM table_name WHERE column_name operator value FirstName Ola Jon Kari Carl Jason Address 120 Main St 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr Example SELECT * FROM Employees WHERE LastName=’Willis' Employee_Id LastName 4 Willis CGS 2835 FirstName Carl Address 12 Bacon Cr City Atlanta Interdisciplinary Web Development City Chicago Tallahassee Tallahassee Atlanta
  40. 40. SELECT Employees Table Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith FirstName Ola Jon Kari Carl Jason Address 120 Main St 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr SELECT column_name(s) FROM table_name WHERE column_name operator value AND/OR column_name operator value Example SELECT * FROM Employees WHERE LastName=’Willis’ OR LastName=‘Pettersen’ Employee_Id LastName 3 Pettersen 4 Willis CGS 2835 FirstName Kari Carl Address City 2413 Sayer Ave Tallahassee 12 Bacon Cr Atlanta Interdisciplinary Web Development City Chicago Tallahassee Tallahassee Atlanta
  41. 41. UPDATE Employees Table Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith FirstName Ola Jon Kari Carl Jason Address 120 Main St 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example UPDATE Employees SET Address=’2727 Monroe St', City=’Tallahassee' WHERE LastName=’Smith' AND FirstName=’Jason' Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith CGS 2835 FirstName Ola Address 120 Main St City Chicago Jon Kari Carl Jason 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr 2727 Monroe St Tallahassee Tallahassee Atlanta Tallahassee Interdisciplinary Web Development City Chicago Tallahassee Tallahassee Atlanta
  42. 42. INSERT INTO Employees Table Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith FirstName Ola Jon Kari Carl Jason Address 120 Main St 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr City Chicago Tallahassee Tallahassee Atlanta INSERT INTO table_name (ColumnName1, … , ColumnNameN ) VALUES (‘data1’, … , ‘dataN’) Example INSERT INTO Employees (LastName, FirstName, Address, City) VALUES (‘Larkin’, ‘Robert’, ‘34 W 7th’, ‘Atlanta’) Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith 6 Larkin CGS 2835 FirstName Ola Jon Kari Address 120 Main St 3 Bogus Dr 2413 Sayer Ave City Chicago Tallahassee Tallahassee Carl Jason Robert 12 Bacon Cr Atlanta 34 W 7th Atlanta Interdisciplinary Web Development
  43. 43. DELETE Employees Table Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 4 Willis 5 Smith FirstName Ola Jon Kari Carl Jason DELETE FROM table_name WHERE some_column=some_value Example DELETE FROM Employees WHERE LastName=’Willis' AND FirstName=’Carl' Employee_Id LastName 1 Baldauf 2 Svendson 3 Pettersen 5 Smith CGS 2835 Address 120 Main St 3 Bogus Dr 2413 Sayer Ave 12 Bacon Cr FirstName Ola Address 120 Main St Jon Kari Jason 3 Bogus Dr Tallahassee 2413 Sayer Ave Tallahassee 2727 Monroe St Tallahassee Interdisciplinary Web Development City Chicago City Chicago Tallahassee Tallahassee Atlanta
  44. 44. PHP > MySQL CGS 2835 Interdisciplinary Web Development
  45. 45. Accessing a MySQL Database from PHP First create a database, table, and fields using phpMyAdmin 1.Establish a connection to mySQL server 2.Select the database 3.Use mysql_query to issue SQL commands CGS 2835 Interdisciplinary Web Development
  46. 46. 1. Establish a Connection mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error()); CGS 2835 Interdisciplinary Web Development
  47. 47. 2. Select the Database mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error()); mysql_select_db(’glm04_webdev') or die(mysql_error()); CGS 2835 Interdisciplinary Web Development
  48. 48. 3. Use mysql_query to Issue Commands mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error()); mysql_select_db(’glm04_webdev') or die(mysql_error()); mysql_query("INSERT INTO visitors (name, email) VALUES('Timmy Mellowman', 'mellowman@fsu.edu' ) ") or die(mysql_error()); CGS 2835 Interdisciplinary Web Development
  49. 49. 3. Use mysql_query to Issue Commands mysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error()); mysql_select_db(’glm04_webdev') or die(mysql_error()); $result = mysql_query("SELECT * FROM visitors") or die(mysql_error()); while($row = mysql_fetch_array( $result )){ echo ”<p> Name: ".$row['name'] ."<br />"; echo "Email: ".$row['email'] ."<br />"; echo " Date: ".$row['date'] .”</p>"; } CGS 2835 Interdisciplinary Web Development
  50. 50. Useful Resources • Tizag PHP/MySQL Tutorial – http://www.tizag.com/mysqlTutorial • W3Schools – PHP MySQL: http://www.w3schools.com/php/php_mysql_intro.asp – SQL: http://www.w3schools.com/sql/default.asp • MySQL Manual: – http://dev.mysql.com/doc/refman/5.0/en • PHP MySQL functions: – http://us3.php.net/manual/en/ref.mysql.php CGS 2835 Interdisciplinary Web Development

×