CGS 2835 Interdisciplinary Web Development Web Database Useful Web Apps +
CGS 2835 Interdisciplinary Web Development
WebDatabaseUseful Web Apps
WebDatabaseUseful Web Apps
++
CGS 2835 Interdisciplinary Web Development
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
AMP
• Apache – open source Web server
• MySQL – open source database
• PHP – programming language that works in HTML to provide interactivity with MySQL
Today’s Dominant Web Architecture
CGS 2835 Interdisciplinary Web Development
AMP
• LAMP (Linux AMP)• SAMP (Solaris AMP)• WAMP (Windows AMP)• MAMP (Mac AMP)• XAMPP (Cross Platform AMP + Perl)
Today’s Dominant Web Architecture
CGS 2835 Interdisciplinary Web Development
DatabaseServer
WebServerHTML REQUEST w/DATAHTML REQUEST w/DATA
http://iSpace.ci.fsu.edu/~username/mysite/index.html?name=geo
SQL CommandSQL Command
PHPPHP
CGS 2835 Interdisciplinary Web Development
DatabaseServer
WebServer
PHPPHP
data
PHP-created HTML
CGS 2835 Interdisciplinary Web Development
MySQLDatabaseServer
PHPPHPdata
PHP-created HTML
SQLSQL
Our Focus
CGS 2835 Interdisciplinary Web Development
Databases
CGS 2835 Interdisciplinary Web Development
File or Table
Database• A collection of data organized to meet user’s needs.
Records (Entities)
Field(Attribute)
CGS 2835 Interdisciplinary Web Development
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 identifies a record– A primary key field prevents duplicate records
from occurring in a table.
CGS 2835 Interdisciplinary Web Development
Primary Keys
Which field would act as the best primary key?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 Interdisciplinary Web Development
Relation Types• One-to-many
– Most typical– Makes use of primary key
• One-to-one• Many-to-many
CGS 2835 Interdisciplinary Web Development
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://tools.ci.fsu.edu
http://localhost
CGS 2835 Interdisciplinary Web Development
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
https://ispace-tools.cci.fsu.edu
MySQL – Creating a Database
CGS 2835 Interdisciplinary Web Development
Click Edit to manage the database with phpMyAdmin
Click Edit to manage the database with phpMyAdmin
You will need this information for use in your PHP
Code
You will need this information for use in your PHP
Code
CGS 2835 Interdisciplinary Web Development
phpMyAdminis 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
Access MySQL Database
We will use phpMyAdmin to:•Create our databases•Create tables in the databases•Create fields in the tables•Manage the database•Examine table data
We will use PHP to:
• Enter records into tables from HTML forms
• Read records from tables and output to HTML pages
CGS 2835 Interdisciplinary Web Development
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-generated data, the mySQL database must be accessed from the HTML code using PHP.
MySQLDatabaseServer
SQLSQLSQLSQLPHPPHP
CGS 2835 Interdisciplinary Web Development
MySQL & phpMyAdmin
• Either method of interacting with the database requires knowledge of SQL
MySQLDatabaseServer
SQLSQLSQLSQLPHPPHP
CGS 2835 Interdisciplinary Web Development
SQL – The Basics
CGS 2835 Interdisciplinary Web Development
Database Strengths
• Data can be sifted, sorted and queried through the use of data manipulation languages.
The power of a database and DBMS lies in the user’s ability to manipulate the data to turn up useful information.
CGS 2835 Interdisciplinary Web Development
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
SQL CommandsSELECT - extracts data from a databaseUPDATE - updates data in a databaseDELETE - deletes data from a databaseINSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new databaseALTER DATABASE - modifies a databaseCREATE TABLE - creates a new tableALTER TABLE - modifies a tableDROP TABLE - deletes a tableCREATE INDEX - creates an index (search key)DROP INDEX - deletes an index
From www.w3schools.com/sql
CGS 2835 Interdisciplinary Web Development
SELECT SELECT field_names(s)FROM table_nameExamples:
SELECT LastName,FirstName FROM Employees
SELECT * FROM Employees
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
LastName FirstName
Baldauf Ola
Svendson Jon
Pettersen Kari
Willis Carl
Smith Jason
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
Employees Table
CGS 2835 Interdisciplinary Web Development
SELECT SELECT column_name(s)FROM table_nameWHERE column_name operator valueExample
SELECT * FROM Employees WHERE LastName=’Willis'
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
Employee_Id LastName FirstName Address City
4 Willis Carl 12 Bacon Cr Atlanta
Employees Table
CGS 2835 Interdisciplinary Web Development
SELECT SELECT column_name(s)FROM table_nameWHERE column_name operator valueAND/OR column_name operator valueExample
SELECT * FROM Employees WHERE LastName=’Willis’ OR LastName=‘Pettersen’
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
Employee_Id LastName FirstName Address City
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
Employees Table
CGS 2835 Interdisciplinary Web Development
UPDATEUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_valueExample
UPDATE EmployeesSET Address=’2727 Monroe St', City=’Tallahassee'WHERE LastName=’Smith' AND FirstName=’Jason'
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
Employees Table
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason 2727 Monroe St Tallahassee
CGS 2835 Interdisciplinary Web Development
INSERT INTOINSERT 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 FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
Employees Table
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
6 Larkin Robert 34 W 7th Atlanta
CGS 2835 Interdisciplinary Web Development
DELETEDELETE FROM table_nameWHERE some_column=some_valueExample
DELETE FROM EmployeesWHERE LastName=’Willis' AND FirstName=’Carl'
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
4 Willis Carl 12 Bacon Cr Atlanta
5 Smith Jason
Employees Table
Employee_Id LastName FirstName Address City
1 Baldauf Ola 120 Main St Chicago
2 Svendson Jon 3 Bogus Dr Tallahassee
3 Pettersen Kari 2413 Sayer Ave Tallahassee
5 Smith Jason 2727 Monroe St Tallahassee
CGS 2835 Interdisciplinary Web Development
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 to mySQL server2.Select the database3.Use mysql_query to issue SQL commands
CGS 2835 Interdisciplinary Web Development
1. Establish a Connectionmysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());
CGS 2835 Interdisciplinary Web Development
2. Select the Databasemysql_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
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', '[email protected]' ) ")or die(mysql_error());
CGS 2835 Interdisciplinary Web Development
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
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