Nothing Special   »   [go: up one dir, main page]

Chapter 04 PHP and Mysql

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 49

Web Programming

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

PHP retrieves MySQL data to produce Dynamic Web Pages


Basic PHP Syntax
 A PHP file normally contains HTML tags,
just like an HTML file, and some PHP
scripting code.
 We have an example of a simple PHP
script which sends the text "Hello World"
to the browser:
The Hello World
<html>
<body>
<?php
echo("Hello World!“);
?>
</body>
</html>
Basic PHP Syntax
 A PHP scripting block always starts with <?php
and ends with ?>.
 A PHP scripting block can be placed anywhere
in the HTML document.
 Each code line in PHP must end with a
semicolon.
 The semicolon is a separator and is used to
distinguish one set of instructions from another.
Variables in PHP
 All variables in PHP start with a $ sign symbol.
 Variables may contain strings, numbers, or
arrays.
<html>
<body>
<?php
$txt="Hello World";
echo($txt);
?>
</body>
</html>
Concatenation
 To concatenate two or more variables
together, use the dot (.) operator
<html>
<body>
<?php
$first_name=“Juan";
$last_name=“Dela Cruz";
echo $first_name . " " . $last_name ;
?>
</body>
</html>
Comments in PHP
 In PHP, we use // to make a single-line
comment or /* and */ to make a large
comment block. <html>
<body>
<?php
//This is a single line
comment
/* This is a
comment block */
?>
</body>
</html>
PHP Operators: Arithmetic
Operator Description Example Result
+ Addition x=2 4
x+2
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
% Modulus (division remainder) 5%2 1
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--
PHP Operators: Assignment
Operator Example Is The Same As

= 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

== is equal to 5==8 returns false

!= is not equal 5!=8 returns true

> is greater than 5>8 returns false

< is less than 5<8 returns true

>= is greater than or equal to 5>=8 returns false

<= is less than or equal to 5<=8 returns true


PHP Operators: Logical
Operator Description Example
&& and x=6
y=3
(x < 10 && y > 1) returns true

|| 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

The SWITCH statement


 Looping Statements
 The WHILE statement

 The DO…WHILE statement

 The FOR statement


The IF Statement
Syntax:
if (condition){
code to be executed if condition is
true;
}
else{
code to be executed if condition is
false;
}
Example: IF Statement
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri"){
echo "Have a nice weekend!";
}else{
echo "Have a nice day!";
}
?>
</body>
</html>
The SWITCH Statement
Syntax:
switch (expression){
case label1: code to be executed if
expression = label1; break;
case label2: code to be executed if
expression = label2; break;
default: code to be executed if
expression is different from both
label1 and label2;
}
Example: SWITCH Statement
<html>
<body>
<?php
switch ($x){
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
The WHILE Statement
 The while statement will execute a block of
code if and as long as a condition is true.
 Syntax:

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

for (initialization; condition; increment)


{
code to be executed;
}
Example: FOR Statement
<html>
<body>
<?php
for ($i=1; $i<=5; $i++){
echo "Hello World!<br/>";
}
?>
</body>
</html>
PHP Functions
 The general syntax form of a function:

<?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".

 The GET Method


 PHP passes the variables along to the web
page’s URL by appending them onto the end
of the URL
PHP and MySQL
 Setup your database in MySQL
 Create a connection from your PHP file to
the database server
 Select the database to be manipulated on
the database server
 Using SQL Query Statements, data may
be retrieved, added, deleted, updated.
PHP Functions for MySQL
 mysql_connect()
 Open a connection to a MySQL Server
 Syntax:
mysql_connect(h_name, u_name, pswrd)
 Example:
$link=mysql_connect(‘localhost’,
‘root’, ‘one’)
PHP Functions for MySQL
 mysql_select_db()
 Selecta MySQL database to manipulate
 Syntax:
mysql_select_db(database_name,connectio
n);
 Example:
$select_db=mysql_select_db(‘sample’,
$dbconn);
PHP Functions for MySQL
 mysql_query()
 Send a MySQL query to the database server
for execution
 Syntax:
mysql_query(query);
 Example:
$query=‘SELECT * FROM tblStudents’;
$result=mysql_query($query);
PHP Functions for MySQL
 mysql_fetch_array()
 Fetcha result row as an associative array, a
numeric array, or both
 Syntax:
 mysql_fetch_array(query);
 Example:
$result = mysql_query("SELECT id, name FROM
mytable");
while ($row = mysql_fetch_array($result)) {
   printf("ID: “ . $row[‘id’]  Name: “ .
$row[‘name’]); 
}
SQL Query Statements
 Retrieving records: SELECT
SELECT * FROM <table_name> WHERE
<condition>
 Adding Records: INSERT
INSERT INTO <table_name>
VALUES(value1, value2,…)
SQL Query Statements
 Deleting Records: DELETE
DELETE <table_name> WHERE
<condition>
 Updating Records: UPDATE
UPDATE <table_name> SET
field1=new_value1,
field2=new_value2, … WHERE
<condition>
What is a Cookie?
 A cookie is often used to identify a user.
 A cookie is a small file that the server
embeds on the user's computer.
 Each time the same computer requests for
a page with a browser, it will send the
cookie too. With PHP, you can both create
and retrieve cookie values.
How to Create a Cookie
 The setcookie() function is used to create
cookies.
Note: The setcookie() function must
appear BEFORE the <html> tag.
 Syntax

setcookie(name, value, expire, path,


domain);
Example: Setting a Cookie
<?php
setcookie("uname", $name, time()+36000);
?>
<html>
<body>
<p>A cookie was set on this page! The
cookie will be active when the client
has sent the cookie back to the
server.</p>
</body>
</html>
How to Retrieve a Cookie Value

 When a cookie is set, PHP uses the


cookie name as a variable.
 To access a cookie you just refer to the
cookie name as a variable.
Tip: Use the isset() function to find out
if a cookie has been set.
Example: Retrieving a Cookie
<html>
<body>
<?php
if (isset($_COOKIE["uname"])){
echo "Welcome " . $_COOKIE["uname"] .
"!<br>";
}else{
echo "You are not logged in!<br>";
}
?>
</body>
</html>
The END

THANK YOU!!!

You might also like