Chapter 04 PHP and Mysql
Chapter 04 PHP and Mysql
What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like
ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL,
Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.)
PHP is an open source software (OSS)
PHP is free to download and use
What is a PHP File?
PHP files may contain text, HTML tags
and scripts
PHP files are returned to the browser as
plain HTML
PHP files have a file extension of ".php",
".php3", or ".phtml"
What is MySQL?
MySQL is a small database server
MySQL is ideal for small and medium
applications
MySQL supports standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
+
PHP combined with MySQL are cross-
platform (means that you can develop in
Windows and serve on a Unix platform)
Why PHP?
PHP runs on different platforms (Windows,
Linux, Unix, etc.)
PHP is compatible with almost all web servers
used today (Apache, IIS, etc.)
PHP is FREE to download from the official PHP
resource: www.php.net
PHP is easy to learn and runs efficiently on the
server side
What do You Need?
PHP Interpreter
Download PHP for free here:
http://www.php.net/downloads.php
MySQL Database
Download MySQL for free here:
http://www.mysql.com/downloads/index.html
Apache Server
Download Apache for free here:
http://httpd.apache.org/download.cgi
Where to Start?
Install an Apache server on a Windows or
Linux machine
Install PHP on a Windows or Linux
machine
Install MySQL on a Windows or Linux
machine
The Process
Web Server
Obtain Data Dynamic Page
PHP
Interpreter
Web Browser
MySQL
Database
PHP File
Page Request
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
PHP Operators: Comparison
Operator Description Example
|| or x=6
y=3
(x==5 || y==5) returns false
! not x=6
y=3
!(x==y) returns true
Control Structures
Conditional Statements
The IF (…ELSE) statement
while (condition){
code block to be executed;
}
Example: WHILE Statement
<html>
<body>
<?php
$i=1;
while($i<=5){
echo "The number is " . $i . "<br/>";
$i++;
}
?>
</body>
</html>
The do...while Statement
The do...while statement will execute a
block of code at least once - it then will
repeat the loop as long as a condition is
true.
Syntax:
do {
code block to be executed;
}while (condition);
Example: DO…WHILE Statement
<html>
<body>
<?php
$i=0;
do{
$i++;
echo "The number is " . $i . "<br/>";
}while ($i<5);
?>
</body>
</html>
The FOR Statement
The for statement is used when you know
how many times you want to execute a
statement or a list of statements.
Syntax
<?php
function functionname($arg1,$arg2,.., $argN)
{
statements;
}
?>
PHP Built-in Functions
The phpinfo() function is used to output
PHP information.
This function is useful for trouble shooting,
providing the version of PHP, and how it is
configured.
PHP provides many functions for ease in
development, consult http://www.php.net
for the list.
PHP Form Handling
The most important thing to notice when
dealing with HTML forms and PHP is that
any form element in an HTML page will
automatically be available to your PHP
scripts.
An HTML Form
<html>
<body>
<form action="welcome.php" method="POST">
Enter your name:<input type="text" name="name">
Enter your age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
The welcome.php File
<html>
<body>
Welcome
<?php
echo $_POST[“name”];
?>
<br>You are
<?php
echo $_POST[“age”];
?>
years old!
</body>
</html>
Form Methods – GET & POST
The POST Method
PHP stores all the "posted" values into an
associative array called "$_POST".
THANK YOU!!!