lecture 4: introduction to php 3 php & mysql. web based 3-tier architecture

18
Lecture 4: Introduction to PHP 3 PHP & MySQL

Post on 21-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Lecture 4:Introduction to PHP 3

PHP & MySQL

Page 2: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Web based 3-tier architecture

Page 3: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

3-tier architecture (application view)

Page 4: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

o the ability to separate logical components of an application ensures that applications are easy to manage and understand.i.e experts can be employed that specialise in one of the layers e.g. user interface design

o because communication can be controlled between each logical tier of an application, changes in one tier, for example, the database access tier, do not have to affect the client componente.g. a change from one DBMS to another would only require a change to the component in the data access layer with little or no effect on the business/logic (middle) or UI layer.

o specific tools and technologies suited to each layer can be deployed (and may evolve at a different pace) .

Advantages of the 3-tier architecture approach

Page 5: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Web based 3-tier architecture: tools & technologies

o Presentation tier – Browser / custom client, Client Side Scripting (JavaScript, ActionScript, VBScript etc.), Applets.

o Logical Tier – Web Server (Apache, IIS, Websphere etc.); Scripting Languages (PHP, Perl etc.), Programming Languages (Java, C, C# etc), Application Frameworks (Ruby on Rails etc.)

o Data Tier – Database Management System (DBMS) (Oracle, MySQL, SQL Server, DB2 etc.), XMLDB

Page 6: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Using MySQL in the persistence tier:

MySQL Strengths

o High performance benchmarks very well against commercial dbs

o Low-cost no cost under open source licence

o Easy to configure and learn easy to set up, SQL compliant

o Portable Linux, Unix and Windows versions

o Open Source source code available for modification

Page 7: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Tools to create a simple web-editable database

o QSEEo MySQL client (or PHPMyAdmin)o PHP Script

Page 8: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Define ER model in QSEE

Generate SQL

Create DatabaseMySQL

Write Script to process DB

Page 9: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

QSEE

• Multi-case diagramming tool– All UML diagrams– ++

• Entity-Relationship Diagrams– Generates SQL for various targets (including MySQL 4.0)– Implements relationships automatically

• 1- many : adds primary key on 1-side as columns in the many side to make foreign keys

• Many-many : adds an new link table with the primary keys from both sides as foreign keys (and a joint primary key)

• Dependent (weak) entities : foreign key becomes part of the primary key

– Sets up the appropriate integrity constraints• Action on delete and update for foreign keys

Page 10: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

PHPMyAdmin

o GUI interface to MySQL server (shares)o GUI runs on stocks (the PHP server)o Full access to a database for

o Creating tableso Added datao Browsing the datao Import and Export of tables…o Execute SQL queries (DML) or definitions (DML)

o But ..

Page 11: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

PHPMyAdmin Interface

Page 12: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Command line MySQL

o Generated SQL comments are rejected by PHPMyAdmin

o login to shares directly using puttyo change directory to your project directoryo Enter:

o mysql –po use databaseo source proj.sql

Page 13: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

Command line MySQL

Page 14: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

PHP/MySQL a simple example (1):

DB Library - Table Book

id title author

1 MySQL in Nutshell Dyer

2 PHP and MySQL Ullman

3 Javascript Smith

Page 15: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

<?php // set up server, username, password & database $host="localhost"; // would be "shares" in cems $un="root"; // would be unix user in cems $pw=""; // would be original cems passsword $db="Library"; // would be unix user name

// connect to the mysql server mysql_connect($host,$un,$pw);

// select the database @mysql_select_db($db) or die( "Unable to select database");

PHP/MySQL a simple example (2):

Page 16: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

// build the query $query="SELECT * FROM book";

// run the query $result=mysql_query($query);

// count the rows returned $num=mysql_numrows($result);

// close the connection mysql_close();

PHP/MySQL a simple example (3):

Page 17: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

// run a loop outputting one row at a time while ($i < $num) { $id=mysql_result($result,$i,"id"); $title=mysql_result($result,$i,"title"); $author=mysql_result($result,$i,"author");

echo "<p>id: $id<br/>Title: $title<br/>Author: $author</p>";

$i++; }?>

PHP/MySQL a simple example (4):

Page 18: Lecture 4: Introduction to PHP 3 PHP & MySQL. Web based 3-tier architecture

PHP/MySQL a simple example (5):