0% found this document useful (0 votes)
20 views

dokumen.tips_php-and-mysql-server-side-scripting-for-web-development

The document provides an overview of PHP and MySQL as essential components for server-side web development, covering topics such as PHP basics, conditional logic, loops, form handling, and object-oriented concepts. It highlights the benefits of using PHP and MySQL, including their open-source nature, versatility, and support for dynamic web pages. Additionally, it explains the setup required for executing PHP scripts and the differences between GET and POST methods for form submission.

Uploaded by

Clash of ClansD
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

dokumen.tips_php-and-mysql-server-side-scripting-for-web-development

The document provides an overview of PHP and MySQL as essential components for server-side web development, covering topics such as PHP basics, conditional logic, loops, form handling, and object-oriented concepts. It highlights the benefits of using PHP and MySQL, including their open-source nature, versatility, and support for dynamic web pages. Additionally, it explains the setup required for executing PHP scripts and the differences between GET and POST methods for form submission.

Uploaded by

Clash of ClansD
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

PHP and MySQL : Server Side

Scripting For Web Development


View PHP & MYSQL Course at: http://www.edureka.co/php-mysql
For Queries: For more details please contact us:
US : 1800 275 9730 (toll free)
Post on Twitter @edurekaIN: #askEdureka
INDIA : +91 88808 62004
Post on Facebook /edurekaIN Email Us : sales@edureka.co
Objectives
At the end of this module, you will be able to understand:

 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

PLATFORM FASTER LARGE


CAPABLE
INDEPENDENT DEVELOPMENTS COMMUNITIES

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

Dynamic and Weak Typing

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.

 PHP stands for Hypertext Preprocessor.

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

 The rendered HTML of the above script looks like below:

<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

 To execute PHP script we need three components to be installed on our computer:

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

 The type of a variable depends upon the value of its values.

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

 In PHP, variables are not required to be declared before assigning a value.

 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

 Following are the various looping statements in PHP:

• 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

 Form is a web page which allows user to enter data.

 Forms contains many elements like text box, text area, checkbox, radio button and submit button

 User enters information in the form elements

 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

 Let us see an example in the upcoming slides

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

 We can send form data to server using two methods


• GET method
• POST method

 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(?)

<form action=“display.php” method=“get”>


Name: <input type=“text” name=“name”><br/>
Email:<input type=“text” name=“email”><br/>
<input type=“submit” value=“submit”>
</form>

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

<form action=“display.php” method=“post”>


Name: <input type=“text” name=“name”><br/>
Email:<input type=“text” name=“email”><br/>
<input type=“submit” value=“submit”>
</form>

 POST Method data can be accessed using $_POST variable

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

• It is much larger limit on the amount of data than can be passed

 Disadvantages:

• The results at a given moment cannot be bookmarked

• 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

 There are two types of function available in PHP

• 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;
}

 Syntax to call a function:

functionName();

 Rules to follow while naming a function:

• Function names are NOT case-sensitive


• Function name starts with a letter or underscore
• Function name cannot start with numbers

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

• Applications can be of any type

• Web based application

• Window based application

• It is used to write programming in object model structure

 Advantages of Object Oriented Programming

• Re-Usability of your code

• Easy to Maintain

• Good Level of Abstraction

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

• Class is declared using class keyword followed by the name


• A set of braces used to declare variables and functions
• Variables can be declared using var keyword followed by $

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

$objectname= new classname();

Example:

$hero = new Pen;


$reynolds= new Pen;

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

$hero -> setPrice("100"); //assigns value 100 to price variable


$hero -> setColor("Green"); //assigns value green to color variable
$hero -> setType("Ink Pen"); //assigns value Ink Pen to type variable
$hero -> setWritingcolor(“blue”); //assigns value blue to writing color of the pen

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

 Two ways to declare constructors


• __construct() function
• Define function using same class name

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

 We can achieve this by using ‘extends’ keyword

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

 Using overriding, we can alter function definition in child class

 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”;
}
}

class childclass extends baseclass {


function two($text) //overriding function2
{
echo "$text ";
}
}
$text = new childclass();
$text->two("Sachin");//it will print Sachin
Slide 41 http://www.edureka.co/php-mysql
Access Modifiers
 Access modifiers is nothing but the level of access and the visibility of the member variables and member
functions

 We can use this access modifiers to show or hide data

 We have three access modifiers in PHP

• Private
• Protected
• Public

Slide 42 http://www.edureka.co/php-mysql
Final Keyword
 Final is a keyword

 If we define the class as final then we cannot extend the class

 If we declare the method as final we cannot override the method

 Syntax for Defining Function as Final

final public function functioname()


{
//function definition comes here
}

 Syntax for Defining Class as Final

final class classname


{
//class definition comes here
}

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

 Hence, the data can easily be accessed, managed, and updated

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 uses standard SQL

 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

• Using mysql_connect() function we can open a database connection


• This function requires five parameters and all are optional

 Syntax

mysql_connect(server,user,pass,new_link,client_flag);

Slide 46 http://www.edureka.co/php-mysql
MySQL Disconnect
 Using PHP Script

• Using mysql_close() function we can disconnect from MySQL server


• This function takes single parameter

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

 This function needs two parameters and returns Boolean value

 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

 Consider we are going to create a database ‘student_details’ to store student information

Slide 48 http://www.edureka.co/php-mysql
Fetching Data
 Select Query

• Data Manipulation –Select Query


• Select statement is used to fetch data from the database
• Fetching data can be simple queries or complex queries

 To select data using PHP script, we can use mysql_query() function

• Write the select query inside the mysql_query() function


• This function is used to execute MySQL queries
• To fetch the selected data using select query we can use two functions

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

Class Recording in LMS

24/7 Post Class Support

Module Wise Quiz

Project Work

Verifiable Certificate
Slide 52 http://www.edureka.co/php-mysql

You might also like