Chapter 1. Laboratory PHP and MySQL CRASH COURSE
Jan 03, 2016
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
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).
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.
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).
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
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.
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.
<?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
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”);
?>
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
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
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
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
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
PHP TAG STYLESSHORT STYLE <? … ?> XML STYLE <?php … ?>SCRIPT STYLE <script language = ‘php’> …
</script>ASP STYLE <% … %>
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