+ All Categories
Home > Technology > PHP and MySQL : Server Side Scripting For Web Development

PHP and MySQL : Server Side Scripting For Web Development

Date post: 15-Aug-2015
Category:
Upload: edureka
View: 946 times
Download: 634 times
Share this document with a friend
53
PHP and MySQL : Server Side Scripting For Web Development View PHP & MYSQL Course at: http://www.edureka.co/php-mysql For more details please contact us: US : 1800 275 9730 (toll free) INDIA : +91 88808 62004 Email Us : [email protected] For Queries: Post on Twitter @edurekaIN: #askEdureka Post on Facebook /edurekaIN
Transcript
Page 1: PHP and MySQL : Server Side Scripting For Web Development

PHP and MySQL : Server Side

Scripting For Web Development

View PHP & MYSQL Course at: http://www.edureka.co/php-mysql

For more details please contact us: US : 1800 275 9730 (toll free)INDIA : +91 88808 62004Email Us : [email protected]

For Queries:Post on Twitter @edurekaIN: #askEdurekaPost on Facebook /edurekaIN

Page 2: PHP and MySQL : Server Side Scripting For Web Development

Slide 2 http://www.edureka.co/php-mysql

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

Page 3: PHP and MySQL : Server Side Scripting For Web Development

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

Page 4: PHP and MySQL : Server Side Scripting For Web Development

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

CAPABLEPLATFORM

INDEPENDENT

SUPPORTS ALL MAJOR WEB

SERVERS

SUPPORTS ALL MAJOR DATABASES

FREE OF COST

FASTER DEVELOPMENTS

LARGE COMMUNITIES

EASYPROVEN AND

TRUSTED

SECURE

Page 5: PHP and MySQL : Server Side Scripting For Web Development

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

Page 6: PHP and MySQL : Server Side Scripting For Web Development

Slide 6 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.

Page 7: PHP and MySQL : Server Side Scripting For Web Development

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

Page 8: PHP and MySQL : Server Side Scripting For Web Development

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

Page 9: PHP and MySQL : Server Side Scripting For Web Development

Slide 9 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.

Page 10: PHP and MySQL : Server Side Scripting For Web Development

Slide 10 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.

Page 11: PHP and MySQL : Server Side Scripting For Web Development

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

Page 12: PHP and MySQL : Server Side Scripting For Web Development

Slide 12 http://www.edureka.co/php-mysql

If-else Statement

<?phpIf($user == “Mohit”){

print “Hello Mohit.”;}else{

print “You are not Mohit.”;}

?>

Page 13: PHP and MySQL : Server Side Scripting For Web Development

Slide 13 http://www.edureka.co/php-mysql

If-elseif Statement

<?phpif($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>”);}

?>

Page 14: PHP and MySQL : Server Side Scripting For Web Development

Slide 14 http://www.edureka.co/php-mysql

Switch Statement

<?phpswitch($day){

case 3:print(“Three golden rings <br>”);break;

case 2:print(“Two golden rings <br>”);break;

default:print(“One golden ring <br>”);

}?>

Page 15: PHP and MySQL : Server Side Scripting For Web Development

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

Page 16: PHP and MySQL : Server Side Scripting For Web Development

Slide 16 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:

<?phpfor($counter=1 //initial expression$counter<4; //termination checks$counter++ //loop-end expressions) {

print(“$counter<br />”);}

?>

Result:

123

Page 17: PHP and MySQL : Server Side Scripting For Web Development

Slide 17 http://www.edureka.co/php-mysql

Foreach Statement

We use foreach loop to iterate through arrays and objects.

Example:

<?php$months = array(“January", “February", “March", “April“, ”May”, ”June”, ”July”, ”August”, ”September”, ”October”, ”November”, ”December”);foreach ($months as $value) {

echo "$value <br>";}?>

Result:

JanuaryFebruaryMarchAprilMayJuneJulyAugust SeptemberOctoberNovemberDecember

Page 18: PHP and MySQL : Server Side Scripting For Web Development

Slide 18 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:

<?php$count=1;While($counter<=6){

print(“Counter value is $counter <br>”);$counter = $counter++;

}?>

Result:

Count value is 1Count value is 2Count value is 3Count value is 4Count value is 5Count value is 6

Page 19: PHP and MySQL : Server Side Scripting For Web Development

Slide 19 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:

<?php$counter=50;do {

print(“Counter value is $counter. <br>”);$counter = $counter + 1;

}While($counter<=10)?>

Result:

Counter value is 50.

Page 20: PHP and MySQL : Server Side Scripting For Web Development

Slide 20 http://www.edureka.co/php-mysql

Break Statement

The break command exits from the inner most loop statements that contain it.

Example:

<?phpfor($x=1; $x<10; $x++) {

If($x % 2 !=0) {break;print(“$x “);

}}?>

Result:

The above code prints nothing because 1 is odd which terminates the for loop immediately.

Page 21: PHP and MySQL : Server Side Scripting For Web Development

Slide 21 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:

<?phpfor($x=1; $x<10; $x++) {

if($x % 2 !=0) {continue;

}print(“$x “);

}?>

Result:

2 4 6 8Here, the continue statement will skip any of odd numbers. It will print only the even numbers.

Page 22: PHP and MySQL : Server Side Scripting For Web Development

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

Page 23: PHP and MySQL : Server Side Scripting For Web Development

Slide 23 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”

Page 24: PHP and MySQL : Server Side Scripting For Web Development

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

<?phpYour First name is: <?php echo $_POST["firstname"]; ?><br>Your Lastname is: <?php echo $_POST["lastname"]; ?>?>

Page 25: PHP and MySQL : Server Side Scripting For Web Development

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

<?phpecho “Name: ”.$_GET*‘name’+;echo “Email: ”.$_GET*‘email’+;?>

Page 26: PHP and MySQL : Server Side Scripting For Web Development

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

Page 27: PHP and MySQL : Server Side Scripting For Web Development

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

<?phpecho “Name: ”.$_POST*‘name’+;echo “Email: ”.$_POST*‘email’+;?>

Page 28: PHP and MySQL : Server Side Scripting For Web Development

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

Page 29: PHP and MySQL : Server Side Scripting For Web Development

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

Page 30: PHP and MySQL : Server Side Scripting For Web Development

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

Page 31: PHP and MySQL : Server Side Scripting For Web Development

Slide 31 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.

Page 32: PHP and MySQL : Server Side Scripting For Web Development

Slide 32 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 callerfunction multiply ($value) {

$value = $value * 10;return $value;

}$retval= multiply (10);Print "Return value is $retval\n";?>

Page 33: PHP and MySQL : Server Side Scripting For Web Development

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

Page 34: PHP and MySQL : Server Side Scripting For Web Development

Slide 34 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 $

<?phpclass classname {

var$var1;var$var2 = "constant text";function myfunc($arg1, $arg2) {

//function code}

}?>

Page 35: PHP and MySQL : Server Side Scripting For Web Development

Slide 35 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;

Page 36: PHP and MySQL : Server Side Scripting For Web Development

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

ExampleAssigning 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

Page 37: PHP and MySQL : Server Side Scripting For Web Development

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

Page 38: PHP and MySQL : Server Side Scripting For Web Development

Slide 38 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 */

}}

Page 39: PHP and MySQL : Server Side Scripting For Web Development

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

}

Page 40: PHP and MySQL : Server Side Scripting For Web Development

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

Page 41: PHP and MySQL : Server Side Scripting For Web Development

Slide 41 http://www.edureka.co/php-mysql

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

Function Overriding

Example:

Page 42: PHP and MySQL : Server Side Scripting For Web Development

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

Page 43: PHP and MySQL : Server Side Scripting For Web Development

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

}

Page 44: PHP and MySQL : Server Side Scripting For Web Development

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

Page 45: PHP and MySQL : Server Side Scripting For Web Development

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

Page 46: PHP and MySQL : Server Side Scripting For Web Development

Slide 46 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);

Page 47: PHP and MySQL : Server Side Scripting For Web Development

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

Page 48: PHP and MySQL : Server Side Scripting For Web Development

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

Page 49: PHP and MySQL : Server Side Scripting For Web Development

Slide 49 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()

Page 50: PHP and MySQL : Server Side Scripting For Web Development

Questions

Slide 50 http://www.edureka.co/php-mysqlTwitter @edurekaIN, Facebook /edurekaIN, use #AskEdureka for Questions

Page 51: PHP and MySQL : Server Side Scripting For Web Development

Slide 51 http://www.edureka.co/php-mysql

Course Topics

Module 1 » PHP Basics and Conditional Logic

Module 2» Functions and Error Handling

Module 3 » Object Oriented Programming

Module 4 » MySQL Installation and Basics

Module 5 » Advance Queries and Data Manipulation

using PHP

Module 6 » MVC Infrastructure Basics & Introduction to

CakePHP

Module 7 » CakePHP Controller, Views and Layout

Module 8» Models and Database Interaction in CakePHP

Module 9» Creating Dynamic Forms using CakePHP Html

Helpers

Module 10» Using MVC & CakePHP to develop a website

Page 52: PHP and MySQL : Server Side Scripting For Web Development

Slide 52

LIVE Online Class

Class Recording in LMS

24/7 Post Class Support

Module Wise Quiz

Project Work

Verifiable Certificate

http://www.edureka.co/php-mysql

How it Works?

Page 53: PHP and MySQL : Server Side Scripting For Web Development

Recommended