+ All Categories
Home > Documents > MySQL & PHP: Part 1: MySQL - · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL...

MySQL & PHP: Part 1: MySQL - · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL...

Date post: 15-Feb-2018
Category:
Upload: vandan
View: 228 times
Download: 4 times
Share this document with a friend
37
Neck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS
Transcript
Page 1: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Neck-Deep in CRUD with MySQL & PHP Part 1: MySQL

Andrew Bullen – ISL Brian Smith – RAILS

Page 2: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS
Page 3: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

LAMP stack

• Linux – operating system • Apache – web server • MySQL – database management system • PHP – programming language

(The “P” could also be PERL or Python.)

Page 4: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

XAMPP

• X – cross-platform • Apache – web server • MySQL – database management system • PHP – programming language • PERL – also included!

Download from apachefriends.org

Page 5: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

MySQL

• “My sequel” (also “My ess cue ell”) • Very widely used • SQL = Structured Query Language • SQL has commands and syntax to define and

manipulate data

Page 6: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

CRUD

• Create – add new data • Read – retrieve existing data • Update – change existing data • Delete – get rid of existing data

Page 7: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Database

• Database has tables • Table has:

– Columns = fields – Rows = records

• Database has relationships between tables – One-to-one – One-to-many – Many-to-many

Page 8: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Relationships

Page 9: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Hey, let’s build a database!

Page 10: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS
Page 11: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Create pet_library database

Page 12: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Create genres table

Page 13: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Define columns for genres table

Page 14: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS
Page 15: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Create pets table

Page 16: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Define columns for pets table

Note: petGenre column needs to be INDEXED.

Page 17: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Add pets -> genres relationship

Page 18: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Set petGenre as a foreign key

Page 19: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Create: Time to add data!

Note: Values of string fields need to be enclosed in single quotes. Do not enclose values of numeric fields in quotes.

Page 20: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Yay!

Page 21: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Let’s add some pets!

Page 22: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Read: Queries to list all pets

SELECT * FROM pets SELECT * FROM pets ORDER BY petName SELECT petName, petDescription FROM pets

Page 23: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Queries to list some pets

SELECT * FROM pets WHERE petName = ‘Spot’ SELECT * FROM pets WHERE petName LIKE ‘S%’ SELECT * FROM pets WHERE petID <> 2

Page 24: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Query with JOIN to include genre

SELECT p.petName, g.genre, p.petDescription FROM pets AS p JOIN genres AS g ON p.petGenre = g.genreID ;

Page 25: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Query for pets and petless genres

SELECT p.petName, g.genre, p.petDescription FROM pets AS p RIGHT JOIN genres AS g ON p.petGenre = g.genreID ;

Page 26: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Update: Wuffles is very old

Page 27: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

R.I.P. Wuffles

Page 28: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Try some more UPDATEs

Some suggestions … • Dr. Moreau has changed one of the cats into a

ferret. • Change the name of a pet. • Bring Wuffles back to life! • Budgie? We say “parakeet” here in the USA!

Page 29: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Delete: Weed dogs from the collection

Everyone have at least one dog? Try this: DELETE FROM genres WHERE genreID = 2 (You should get an error.) Now do: DELETE FROM pets WHERE petGenre = 2 And then try again: DELETE FROM genres WHERE genreID = 2

Page 30: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Be careful with DELETE

• If you don’t include a good WHERE condition, you may delete rows that you needed to keep.

Some other dangerous MySQL commands: • TRUNCATE TABLE – deletes all data from table,

but keeps table structure • DROP TABLE – totally deletes table • DROP DATABASE – Guess what that does?

Page 31: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Make regular backups!

Page 32: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Database user accounts

• XAMPP’s default is no password for root user • In the real world:

– Set a strong password for root. Instructions at http://www.scriptarticle.com/2012/07/04/how-to-change-or-reset-xampp-mysql-root-password/

– Add a new user with access only for your database, and connect your website code to the database as that user.

Page 33: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Adding user for pet_library

Page 34: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS
Page 35: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Allow only necessary permissions

Page 36: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Help and tutorials:

• http://dev.mysql.com/doc/ • http://stackoverflow.com/ • www.w3schools.com/sql/

Page 37: MySQL & PHP: Part 1: MySQL -   · PDF fileNeck-Deep in CRUD with MySQL & PHP Part 1: MySQL Andrew Bullen – ISL Brian Smith – RAILS

Recommended