170 likes | 316 Views
PHP and MySQL . Code Reuse, OO, Error Handling and MySQL (Intro). r equire() and include(). Software Engineering tenets tell us to reuse code rather than write new code. Lower Cost Increased reliability (in theory!) Improved consistency Code reuse in PHP ( home.html vs home.php )
E N D
PHP and MySQL Code Reuse, OO, Error Handling and MySQL (Intro)
require() and include() • Software Engineering tenets tell us to reuse code rather than write new code. • Lower Cost • Increased reliability (in theory!) • Improved consistency • Code reuse in PHP (home.htmlvshome.php) • require(‘<filename>’) • include(‘<filename>’)
Object-Orientation • What is it? • Classes – defines abstract characteristics of a thing • State (data) • Behavior (procedure/methods) • Identity (unique identifier for an instance of the class) • Polymorphism - ability to create a variable, a function, or an object that has more than one form • Dog, cat and bird can all “talk” but does so differently. Same function call, different result. • Operator overloading – (+, -, * and /) can be performed differently in classes. • Inheritance – create a new class as an extension of an existing class • class Student extends Person
Object Orientation in PHP • home.php and services.php • Both use Page class defined in page.inc • Very simple to create and rapidly deploy pages with the same look and feel • services.php uses inheritance to extend Page class to reuse code and only add code that is unique to that page!
Exception Handling • Exception handling - mechanism designed to handle the occurrences that change the normal flow of program execution. • Basic Concepts • try { //code goes here } • catch{ //handle exception} • Back to Bob’s Auto Parts… • Handling file open exceptions…
MySQL • SQL – Structured Query Language • Based on Relational Algebra • Data is structured into: • Databases – a group of related tables • Tables – a container of related Tuples • Tuples – a set of individual values • Keys – a way to uniquely identify tuples
MySQL • To create tables you need to be familiar with Database Normalization • Gather like data together to form a table • Look to avoid anomalies and wasted space • Insertion anomaly • Modification anomaly • Deletion anomaly • Atomic values – each column stores only 1 instance of a type of information
MySQL • Choose sensible keys • A key of a last name will limit your customer base! • Think about what you want to ask the DB – make sure you capture the info to answer the questions! • What are my top sellers? • Who are my best customers? • Avoid designs that create empty column • Books(ISBN, Author, Title, Price, Review)
MySQL DB Design • Use the KISS rule. • Create tables that mirror real world objects • Avoid anomalies • Keys can be used to link tables together
MySQL • Accessing your CISC474 MySQL database… • A database has been created for each of you to use. • Username/DB Name: Your ECE/ACAD username OR first initial/last name (John Smith = jsmith). • Password: Your username + last 4 digits of your Student ID… • Change it ASAP to something easy to remember but difficult for others to guess… • Do not use a password that you would not either the TA or myself to see since you will need to enter it into PHP scripts!
MySQL • Change password: SET PASSWORD = PASSWORD(‘newpass’); • Great source for MySQL (we are using version 5.1) • http://dev.mysql.com/doc/refman/5.1/en/index.html
MySQL - Creating tables • Logon to cisc474.acad.cis.udel.edu mysql –u <username> -p • You will be prompted for your password • To open your db type use <username> • To create a table • http://dev.mysql.com/doc/refman/5.1/en/create-table.html • bookorama.sql
MySQL Create tables • To run a sql script source <filename> • Look up table info with SHOW and DESCRIBE • Get up to speed with INDEX • http://dev.mysql.com/doc/refman/5.1/en/create-index.html • MySQL datatypes • http://dev.mysql.com/doc/refman/5.1/en/data-type-overview.html
Working with MySQL Data • Insert • Select • Cartesian product (aka Full Join, Cross Join) • Inner join • Equi-join • Outer Join (Left, Right) • Update • Alter • Delete
MySQL and PHP • Now that you have mastered the basics of MySQL… • How do you put PHP and MySQL together… • It is quite simple! • Search and newbook examples!
Building eCommerce Sites • What makes a site fail?