dokumen.tips_php-and-mysql-server-side-scripting-for-web-development
dokumen.tips_php-and-mysql-server-side-scripting-for-web-development
Basics of PHP
Conditional Logic and Loops
PHP Form Handling
PHP Functions
Object Oriented Concepts
Implement MySQL with PHP
Slide 2 http://www.edureka.co/php-mysql
PHP & MySQL - Overview
PHP & MySQL is an open-source
PHP & MySQL are two key components in the open-source LAMP stack
It is the most appropriate tool for developing dynamic web pages. For example, we can develop informative forums,
chatting platforms, e-commerce shopping carts, CRM solutions, community websites and database driven sites
PHP with MySQL is a powerful combination showing the real power of Server-Side scripting
PHP has a wide range of MySQL functions available with the help of a separate module
Slide 3 http://www.edureka.co/php-mysql
Benefits of PHP & MySQL
PHP web development means developing websites and dynamic web pages using the versatile and capable server-side
scripting language
FREE OF
SECURE
COST
SUPPORTS ALL
SUPPORTS ALL PROVEN AND
MAJOR WEB EASY
MAJOR DATABASES TRUSTED
SERVERS
Slide 4 http://www.edureka.co/php-mysql
Intricacies of PHP & MySQL
Variable Variables
Dynamic Arrays
Dynamic Constants
Dynamic Functions
Dynamic Code
Dynamic Includes
Built-in Functions
Superglobals
Slide 5 http://www.edureka.co/php-mysql
What is PHP?
PHP is the web development language written by and for web developers.
It was originally named as Personal Home Page Tools and later on as Professional Home Page.
It is a server-side script, which can be embedded in HTML or used as a standalone program script.
Slide 6 http://www.edureka.co/php-mysql
Script in PHP
PHP can be embedded in HTML or can be written as stand-alone program by using special markup tags. They are:
<?php
//PHP code comes here
?>
We can write PHP script in always available Notepad or get some PHP specific IDE downloaded from internet.
We can call the file from web browser after saving it using .php extension in webserver directory.
Slide 7 http://www.edureka.co/php-mysql
PHP Example
<html>
<head>
<title>PHP Example</title>
<body>
<?php echo “This is my first PHP script."; ?>
</body>
</html>
<html>
<head>
<title>PHP Example</title>
<body>
<p> This is my first PHP script.</p>
</body>
</html>
Slide 8 http://www.edureka.co/php-mysql
Environment Setup
Web Server - PHP supports many web server, including Apache server and IIS.
Database - PHP supports many databases. But the extensively used is MySQL.
PHP Parser - A Parser is required to generate HTML output from PHP code.
Slide 9 http://www.edureka.co/php-mysql
PHP Variables
A variable in any programming language is a name to store a value that can be referenced later as required.
In PHP, Variables are defined a name preceded by a dollar sign ($). Eg. $firstName, $last_Name etc.
Equal to (=) operator is used to assign a value to a variable name, on the left-hand side and the value on the
right-hand side.
Data type for a variables is not required to be declared for a variable in PHP. Depending upon the value it is
automatically interpreted.
Slide 10 http://www.edureka.co/php-mysql
Decision Making Statements
Statements that are used to perform certain functions depending on certain conditions are called Decision
making statements as given below:
• If…else statement
• elseif statement
• Switch statement
Slide 11 http://www.edureka.co/php-mysql
If-else Statement
<?php
If($user == “Mohit”)
{
print “Hello Mohit.”;
}
else
{
print “You are not Mohit.”;
}
?>
Slide 12 http://www.edureka.co/php-mysql
If-elseif Statement
<?php
if($day ==5) {
print(“Five team members. <br>”);
} elseif($day ==4) {
print(“Four team members <br>”);
} elseif($day ==3) {
print(“Three team members <br>”);
} elseif($day ==2) {
print(“Two team members <br>”);
} elseif($day ==1) {
print(“One team members <br>”);
}
?>
Slide 13 http://www.edureka.co/php-mysql
Switch Statement
<?php
switch($day)
{
case 3:
print(“Three golden rings <br>”);
break;
case 2:
print(“Two golden rings <br>”);
break;
default:
print(“One golden ring <br>”);
}
?>
Slide 14 http://www.edureka.co/php-mysql
Looping Statements
• For loop
• Foreach loop
• While loop
• Do…while loop
Slide 15 http://www.edureka.co/php-mysql
For Statement
A for statement execution starts with evaluation of initial-expression, which is initialization of counter variable .
Then evaluation of termination-check is done. if false, the for statement concludes, and if true, the statement
executes.
Finally, the loop-end-expression is executed and the loop begins again with termination–check.
Example: Result:
<?php 1
for($counter=1 //initial expression 2
$counter<4; //termination checks 3
$counter++ //loop-end expressions) {
print(“$counter<br />”);
}
?>
Slide 16 http://www.edureka.co/php-mysql
Foreach Statement
We use foreach loop to iterate through arrays and objects.
Example: Result:
<?php January
February
$months = array(“January", “February", “March", “April“,
March
”May”, ”June”, ”July”, ”August”, ”September”, ”October”, April
”November”, ”December”); May
foreach ($months as $value) { June
echo "$value <br>"; July
} August
?> September
October
November
December
Slide 17 http://www.edureka.co/php-mysql
While Statement
The while loop evaluates the condition expression as Boolean. if true, it executes statements and then starts
again by evaluating condition. If the condition is false, the loop terminates.
Example:
Result:
<?php
Count value is 1
$count=1;
Count value is 2
While($counter<=6)
Count value is 3
{
Count value is 4
print(“Counter value is $counter <br>”);
Count value is 5
$counter = $counter++;
Count value is 6
}
?>
Slide 18 http://www.edureka.co/php-mysql
Do-While Statement
The only difference between while and do-while is that the do-while will execute the statement at least once.
The statement is executed once, and then the expression is evaluated. If the expression is true, the statement is
repeated until the expression becomes false.
Example:
Result:
<?php
$counter=50; Counter value is 50.
do {
print(“Counter value is $counter. <br>”);
$counter = $counter + 1;
}
While($counter<=10)
?>
Slide 19 http://www.edureka.co/php-mysql
Break Statement
The break command exits from the inner most loop statements that contain it.
Example:
Result:
<?php
for($x=1; $x<10; $x++) { The above code prints nothing because
If($x % 2 !=0) { 1 is odd which terminates the for loop
break; immediately.
print(“$x “);
}
}
?>
Slide 20 http://www.edureka.co/php-mysql
Continue Statement
The continue command skips to the end of the current iteration of the innermost loop that contains it.
Example:
Result:
<?php
for($x=1; $x<10; $x++) { 2468
if($x % 2 !=0) { Here, the continue statement will skip any
continue; of odd numbers. It will print only the even
} numbers.
print(“$x “);
}
?>
Slide 21 http://www.edureka.co/php-mysql
PHP Forms
Forms contains many elements like text box, text area, checkbox, radio button and submit button
And, the entered information are sent to the server for processing
Using HTML we can create forms and using PHP we can process form elements
Slide 22 http://www.edureka.co/php-mysql
HTML Form
See the below example for HTML form with two text boxes and one submit button
<html>
<body>
<form action=“save.php" method="post">
First Name: <input type="text" name=“firstname"><br>
Last Name: <input type="text" name=“lastname"><br>
<input type="submit">
</form>
</body>
</html>
The user enters the above information and clicks the submit button, the information is sent to a file called
“save.php”
Slide 23 http://www.edureka.co/php-mysql
Processing Forms
The form data is sent to a PHP file for processing
In the previous code, we used POST method to send data. See the below example, to display the submitted data.
To print the values, use the below code in save.php
<?php
Your First name is: <?php echo $_POST["firstname"]; ?>
<br>
Your Lastname is: <?php echo $_POST["lastname"]; ?>
?>
Slide 24 http://www.edureka.co/php-mysql
Get Method
GET method passes argument from one page to the next page.
It appends the indicated variable name(s) and values(s) to the URL. The value and the page name separated by
question-mark(?)
<?php
echo “Name: ”.$_GET*‘name’+;
echo “Email: ”.$_GET*‘email’+;
?>
Slide 25 http://www.edureka.co/php-mysql
Get Method (Continued)
Advantage:
• It constructs an actual new and differentiable URL query string. Users can now bookmark this page
Disadvantage:
• The GET method is not suitable for logins because the username and password are fully visible onscreen as
well as potentially stored in the client browser’s memory as a visited page
• Every GET submission is recorded in the web server log, data set included
• Because the GET method assigns data to a server environment variable, the length of the URL is limited
Slide 26 http://www.edureka.co/php-mysql
Post Method
POST is the preferred method of form submission
The form data is included in the body of the form when it is forwarding to the processing agent. There will be no
change in the URL
<?php
echo “Name: ”.$_POST*‘name’+;
echo “Email: ”.$_POST*‘email’+;
?>
Slide 27 http://www.edureka.co/php-mysql
Post Method (Continued)
Advantages:
• It is more secure than GET because user-entered information is never visible in the URL
Disadvantages:
• This methods can be incompatible with certain firewall setups, which strip the form data as a security
measure
Slide 28 http://www.edureka.co/php-mysql
PHP Functions
A function is a set of codes which are used to perform some specific tasks
Its main advantage is reusability. Instead of defining a code repeatedly, we can create functions and use them
when needed
The function will not execute directly when the program loads. We need to call a function
• Built-in functions - The real power of PHP is its functions. PHP has more than 1000 built-in functions. They
can be invoked directly
• User defined functions - We can also create our own functions. We will discuss about the creation of our
own functions in next slide
Slide 29 http://www.edureka.co/php-mysql
PHP Functions Syntax
Example:
function functionName() {
set of code to be executed;
}
functionName();
Slide 30 http://www.edureka.co/php-mysql
PHP Functions Return Value
Function can return a value. It will return one value or more than one value using array
It returns the value using the return keyword. If the return statement is found in the function it will stop
execution and send the value to the callback function.
Slide 31 http://www.edureka.co/php-mysql
Function Parameters
Function parameters are variables passes to the function inside the parentheses. They are declared much like a
typical variable would be:
<?php
// multiply a value by 10 and return it to the caller
function multiply ($value) {
$value = $value * 10;
return $value;
}
$retval= multiply (10);
Print "Return value is $retval\n";
?>
Slide 32 http://www.edureka.co/php-mysql
Object Oriented Concepts
Object Oriented Programming (OOP) is a programming concept used to design our application
• Easy to Maintain
Slide 33 http://www.edureka.co/php-mysql
Classes
Defining PHP Classes
• Class is a user defined data type which includes functions and member variables
• It is used to define object. It is the blueprint of the object
Class Declaration
<?php
class classname {
var$var1;
var$var2 = "constant text";
function myfunc($arg1, $arg2) {
//function code
}
}
?>
Slide 34 http://www.edureka.co/php-mysql
Creating Objects in PHP
In Object Oriented language, properties are called member variables. And, behaviors are called member
functions
• Once we defined our class, then we can create as many objects using the new operator
• Pen is class, Hero pen, Reynolds etc are called its objects
Syntax
Example:
Slide 35 http://www.edureka.co/php-mysql
Calling Member Functions
After creating objects, we can access our member functions
We can only access the member function of the class of which we created objects
Let us see how to access member functions using the objects $hero, $reynolds
Example
Assigning values to the object $hero by accessing its member functions
Slide 36 http://www.edureka.co/php-mysql
Constructor Functions
Constructor is a special type of function
It is automatically invoked when an object is created. So we can use this function for initialization
To define a constructor, PHP provides a special function called __construct(). We can pass any number of
arguments to this function
A function can also become a constructor, if it defined by the same name of the class
Slide 37 http://www.edureka.co/php-mysql
Constructor Functions (Continued)
Method 1:
class classname
{
function __construct(p1, p2, ..., pN]){
/* Class initialization code */
}
}
Method 2:
class classname
{
function classname(p1, p2, ..., pN]){
/* Class initialization code */
}
}
Slide 38 http://www.edureka.co/php-mysql
Inheritance
Inheritance is the method of inheriting one class properties to other class
Class parentclass
{
//parent class definition
}
Class childclass extends parentclass
{
//child class definition
}
Slide 39 http://www.edureka.co/php-mysql
Function Overriding
Function overriding is nothing but overriding the function of parent class to child class and modify those
functions
To override, we need to create same function in sub class which it is in base class
Slide 40 http://www.edureka.co/php-mysql
Function Overriding
Example:
class baseclass {
public function one() {
echo “First function”;
}
public function two() {
echo “second function”;
}
}
• Private
• Protected
• Public
Slide 42 http://www.edureka.co/php-mysql
Final Keyword
Final is a keyword
Slide 43 http://www.edureka.co/php-mysql
Database
A database is a unique application that organizes a group of data
Each database application has one or more APIs for creating, managing, accessing, searching and duplicating the
data it holds
Slide 44 http://www.edureka.co/php-mysql
MySQL Introduction
MySQL is a database system which is used on the web and runs on the server
It is very fast, reliable and easy to use and also it is ideal for both small and large applications
Slide 45 http://www.edureka.co/php-mysql
MySQL Connection
Using PHP Script
Syntax
mysql_connect(server,user,pass,new_link,client_flag);
Slide 46 http://www.edureka.co/php-mysql
MySQL Disconnect
Using PHP Script
Syntax
mysql_close(resource $link_identifier);
If we did not specify any parameter then the last opened database is closed
Slide 47 http://www.edureka.co/php-mysql
Execute MySQL Queries
Using PHP function mysql_query() we can run a MySQL query.
Function Syntax
mysql_query(sql_query, connection);
First parameter is mandatory. sql_query parameter is mandatory. It specifies the original query to be executed
The second parameter is optional. It is the connection parameter. If we did not specified, it takes the last opened
connection
Slide 48 http://www.edureka.co/php-mysql
Fetching Data
Select Query
mysql_fetch_array()
mysql_fetch_assoc()
Slide 49 http://www.edureka.co/php-mysql
Questions
Slide 50 Twitter @edurekaIN, Facebook /edurekaIN, use #AskEdureka for Questions http://www.edureka.co/php-mysql
Course Topics
Module 1 Module 6
» PHP Basics and Conditional Logic » MVC Infrastructure Basics & Introduction to
CakePHP
Module 2
» Functions and Error Handling Module 7
» CakePHP Controller, Views and Layout
Module 3
» Object Oriented Programming Module 8
» Models and Database Interaction in CakePHP
Module 4
» MySQL Installation and Basics Module 9
» Creating Dynamic Forms using CakePHP Html
Module 5 Helpers
» Advance Queries and Data Manipulation
using PHP Module 10
» Using MVC & CakePHP to develop a website
Slide 51 http://www.edureka.co/php-mysql
How it Works?
LIVE Online Class
Project Work
Verifiable Certificate
Slide 52 http://www.edureka.co/php-mysql