SlideShare a Scribd company logo
1 of 49
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
P H P
Interpreter
PHP File
Web Browser
MySQL
Database
Page Request
Obtain Data Dynamic Page
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
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4
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 GET Method
PHP stores all the "posted" values into an
associative array called "$_POST".
 The POST 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()
Select a MySQL database to manipulate
Syntax:
mysql_select_db(database_name,connectio
n);
Example:
$select_db=mysql_select_db(‘sample’,$db
conn);
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()
Fetch a 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!!!

More Related Content

Similar to PHP and MySQL.ppt

Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009cwarren
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessoradeel990
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPprabhatjon
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners musrath mohammad
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHPNat Weerawan
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting languageElmer Concepcion Jr.
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)Guni Sonow
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomerShivi Tomer
 

Similar to PHP and MySQL.ppt (20)

PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Basic php 5
Basic php 5Basic php 5
Basic php 5
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 
Php
PhpPhp
Php
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009
 
PHP
PHPPHP
PHP
 
PHP Hypertext Preprocessor
PHP Hypertext PreprocessorPHP Hypertext Preprocessor
PHP Hypertext Preprocessor
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
Day1
Day1Day1
Day1
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)basic concept of php(Gunikhan sonowal)
basic concept of php(Gunikhan sonowal)
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomer
 

Recently uploaded

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 

PHP and MySQL.ppt

  • 1.
  • 2. 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
  • 3. 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"
  • 4. 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
  • 5. +  PHP combined with MySQL are cross- platform (means that you can develop in Windows and serve on a Unix platform)
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. The Process Web Server P H P Interpreter PHP File Web Browser MySQL Database Page Request Obtain Data Dynamic Page PHP retrieves MySQL data to produce Dynamic Web Pages
  • 10. 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:
  • 11. The Hello World <html> <body> <?php echo("Hello World!“); ?> </body> </html>
  • 12. 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.
  • 13. 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>
  • 14. 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>
  • 15. 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>
  • 16. PHP Operators: Arithmetic Operator Description Example Result + Addition x=2 x+2 4 - Subtraction x=2 5-x 3 * Multiplication x=4 x*5 20 / Division 15/5 5/2 3 2.5 % Modulus (division remainder) 5%2 10%8 10%2 1 2 0 ++ Increment x=5 x++ x=6 -- Decrement x=5 x-- x=4
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. Control Structures  Conditional Statements The IF (…ELSE) statement The SWITCH statement  Looping Statements The WHILE statement The DO…WHILE statement The FOR statement
  • 21. The IF Statement Syntax: if (condition){ code to be executed if condition is true; } Else{ code to be executed if condition is false; }
  • 22. 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>
  • 23. 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; }
  • 24. 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>
  • 25. 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; }
  • 26. Example: WHILE Statement <html> <body> <?php $i=1; while($i<=5){ echo "The number is " . $i . "<br/>"; $i++; } ?> </body> </html>
  • 27. 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);
  • 28. Example: DO…WHILE Statement <html> <body> <?php $i=0; do{ $i++; echo "The number is " . $i . "<br/>"; }while ($i<5); ?> </body> </html>
  • 29. 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; }
  • 30. Example: FOR Statement <html> <body> <?php for ($i=1; $i<=5; $i++){ echo "Hello World!<br/>"; } ?> </body> </html>
  • 31. PHP Functions  The general syntax form of a function: <?php function functionname($arg1,$arg2,.., $argN) { statements; } ?>
  • 32. 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.
  • 33. 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.
  • 34. 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>
  • 35. The welcome.php File <html> <body> Welcome <?php echo $_POST[‘name’]; ?> <br>You are <?php echo $_POST[‘age’]; ?> years old! </body> </html>
  • 36. Form Methods – GET & POST  The GET Method PHP stores all the "posted" values into an associative array called "$_POST".  The POST Method  PHP passes the variables along to the web page’s URL by appending them onto the end of the URL
  • 37. 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.
  • 38. 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’)
  • 39. PHP Functions for MySQL  mysql_select_db() Select a MySQL database to manipulate Syntax: mysql_select_db(database_name,connectio n); Example: $select_db=mysql_select_db(‘sample’,$db conn);
  • 40. 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);
  • 41. PHP Functions for MySQL  mysql_fetch_array() Fetch a 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’]); }
  • 42. SQL Query Statements  Retrieving records: SELECT SELECT * FROM <table_name> WHERE <condition>  Adding Records: INSERT INSERT INTO <table_name> VALUES(value1, value2,…)
  • 43. 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>
  • 44. 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.
  • 45. 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);
  • 46. 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>
  • 47. 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.
  • 48. 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>