Top Banner
Chapter 1. Laboratory PHP and MySQL CRASH COURSE
30

Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Jan 03, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Chapter 1. LaboratoryPHP and MySQL CRASH COURSE

Page 2: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Overview:1. Discussion of the basic architecture of a

web application.2. Discussion of the relevance of using MySQL

and PHP in a web application.

Page 3: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Basic Architecture of a Web Application

Additional considerations in the architecture

ClientServerOperating System

Page 4: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

TASKS OF THE WEB SERVERThe Web server has what seems to be a fairly

straightforward job. It sits there, running on top of your operating system, listening for requests that somebody on the Web might make, responding to those requests, and serving out the appropriate Web pages. In reality, it is a bit more complicated than that, and because of the 24/7 nature of the Web, the stability of the Web server is a major issue. There are many Web servers out there, but two dominate the market. These are Apache and Microsoft’s Internet Information Server (IIS).

Page 5: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

TASKS OF THE MIDDLEWAREThe middleware work closely with the Web

server to interpret the requests made from the World Wide Web, process these requests, interact with other programs on the server to fulfill the requests, and then indicate to the Web server exactly what to serve to the client’s browser.

Page 6: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

RELATIONAL DATABASESRelational database management systems

(RDBMSes) provide a great way to store and access complex information. They have been around for quite a while. In fact, they predate the Web, Linux, and Windows, so it should be no surprise that there are many RDBMSes to choose from. All the major databases make use of the Structured Query Language (SQL).

Page 7: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Tools to check before running PHP application (with MySQL).In PHP you should have the following software:

1. PHP code editor (IDE).2. PHP compiler/interpreter.3. Web Server

While in MySQL:1. Driver2. Server3. The MySQL Database Manager

Page 8: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

The PHP scriptNow’s the time to move to the text editor. In

the course of configuring your Web server, you need to let it know which files should be handed off to PHP so the engine can interpret the page. Most often, these files have a .php extension, though it is possible to have PHP interpret anything, including .html files. These scripts live inside the folder designated to hold Web pages. For Apache, this is usually /htdocs.

Page 9: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

BASIC SYNTAXOne neat thing about PHP is that it lets you move between straight HTML and commands that are part of the PHP programming language. It works like this: The sections of your script between the opening tag (<?php) and the closing tag (?>) are interpreted by the PHP engine, and portions not within these tags are treated as plain HTML. Check out the following PHP page.

Page 10: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

<?phpecho “Hi, mom. “;$var = date(“H”);if ($var <= 11){echo “good morning”;}elseif ($var > 11 and $var < 18){echo “good afternoon”;}else{echo “good evening”;}?>

<?phpecho “Hi”;?>mom.

Code Overview

Page 11: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Connecting to the Database (PHP to MySQL)<?phpmysql_connect(“localhost”,”nobody”,”ydobon”)

or die(“<h3>could not connect toMySQL</h3>\n”);

mysql_select_db(“guestbook”) or die(“<h3>could not select database ‘guestbook’</h3>\n”);

?>

Page 12: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Working with PHPGetting Started with PHP – variables

Data typesOperators

Control StructureIf statementelse statementelseif statementSwitch statementIteration

while loop for loop do .. While loops

PHP’s built-in functionWriting organized and readable code

Page 13: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Assigning Simple VariablesWithin a Script$a = “this is a string”; //this is a string$b = 4; //this is an integer$c = 4.837; //this is a floating-point number$d = “2”; //this is another stringTyping is flexible, and PHP is pretty smart about

handling changes in types. Fore example, given the code you just saw, the following would evaluate as you’d probably hope:

$e = $b + $d;echo $e; answer: 6

Page 14: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Another example:$a = 2;$b = “2 little piggies”;$c = $a + $b; answer: $c=4

$f = 2; //$f is an integer$g = 1.444; // $g is a double (floating-point)

type$f = $f + $g; //$f is now a double type answer:

3.444

Page 15: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP will also do type conversions when comparing two variables. This is a goodthing, because the most common values for a script, entries submitted from anHTML form, always come in as strings. Here’s an example:

$a = ‘1.3’;if ($a == 1.3){echo “‘$a’ is 1.3\n”;}else{echo “‘$a’ is not 1.3\n”;}

answer: ‘1.3’ is 1.3

Page 16: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

If you need to make a strict comparison, where the types as well as the valuesmust match, you can use a triple equal sign (===) operator (or its inverse, !==). Thismost commonly arises when you need to distinguish between 0, NULL, FALSE, andan empty string, since in a normal comparison these will all be treated as equal. Thefollowing code demonstrates:

$a = 0;if ($a === 0){echo “‘$a’ is 0\n”;}else{echo “‘$a’ is not 0\n”;}if ($a === FALSE){echo “‘$a’ is FALSE\n”;}else{echo “‘$a’ is not FALSE\n”;}

The result: ‘0’ is 0 ‘0’ is not FALSE

Page 17: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Delimiting strings$my_name = “Jay”;$phrase = “Hello, my name is $my_name”;echo $phrase;

Page 18: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP TAG STYLESSHORT STYLE <? … ?> XML STYLE <?php … ?>SCRIPT STYLE <script language = ‘php’> …

</script>ASP STYLE <% … %>

Page 19: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP’s data typePHP supports the following data types:

1. Integer – wholes numbers2. Double – real numbers3. String – string characters4. Array – use for multiple storage of values5. Object – use to store instances of classes

Page 20: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP’s arithmetic operators

Page 21: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP’s string operator

Page 22: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP’s combined arithmetic operations

Page 23: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP’s comparison operators

Page 24: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

PHP’s logical operators

Page 25: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Control Structureif

else

Page 26: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

Continued…

elseif

Page 27: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

switch case

Page 28: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

ITERATIONwhile loop

Page 29: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

for loop

for example

Page 30: Overview: 1. Discussion of the basic architecture of a web application. 2. Discussion of the relevance of using MySQL and PHP in a web application.

do..while loop

example