+ All Categories
Home > Technology > PHP MySQL Workshop - facehook

PHP MySQL Workshop - facehook

Date post: 18-Nov-2014
Category:
Upload: brainware-consultancy-pvt-ltd
View: 2,107 times
Download: 6 times
Share this document with a friend
Description:
For PCMT Students
Popular Tags:
57
2. PHP & MySQL – An Introduction 2. PHP & MySQL – An Introduction 3. Getting started 3. Getting started 4. Examples 4. Examples 1. Current trends – WEB Tech… 1. Current trends – WEB Tech… 5. Simple Registration & Login ap 5. Simple Registration & Login app
Transcript
Page 1: PHP MySQL Workshop - facehook

2. PHP & MySQL – An Introduction2. PHP & MySQL – An Introduction2. PHP & MySQL – An Introduction2. PHP & MySQL – An Introduction

3. Getting started3. Getting started3. Getting started3. Getting started

4. Examples4. Examples4. Examples4. Examples

1. Current trends – WEB Tech…1. Current trends – WEB Tech…1. Current trends – WEB Tech…1. Current trends – WEB Tech…

5. Simple Registration & Login app5. Simple Registration & Login app5. Simple Registration & Login app5. Simple Registration & Login app

Page 2: PHP MySQL Workshop - facehook

Usage of server-side programming languages for websites

Source : http://w3techs.com

PHP is now installed on more than

20 million websites20 million websites and 1 million web servers1 million web servers.

Page 3: PHP MySQL Workshop - facehook

Usage of client-side programming languages for websites

Source : http://w3techs.com

Page 4: PHP MySQL Workshop - facehook

Usage of markup languages for websites

Source : http://w3techs.com

Page 5: PHP MySQL Workshop - facehook

What is XHTML ?

1. XHTML is an Extensible HyperText Markup Language

2. XHTML is a combination of HTML and XML

(EXtensible Markup Language)

3. XHTML is almost identical to HTML 4.01 but a stricter

cleaner version. The major differences are: XHTML elements must be properly nested. XHTML elements must always be closed. XHTML elements must be in lowercase. XHTML documents must have one root element.

Page 6: PHP MySQL Workshop - facehook

Usage of JavaScript libraries for websites

Source : http://w3techs.com

Page 7: PHP MySQL Workshop - facehook

PHP: Hypertext Preprocessor

• PHP was created by Rasmus Lerdorf in 1994.

• PHP is a recursive acronym for “PHP: Hypertext Preprocessor”.

• PHP is powerful server-side scripting language for creating dynamic and interactive websites.

• PHP is perfectly suited for Web development and can be embedded directly into the HTML code.

• The PHP syntax is very similar to JavaScript, Perl and C Language.

Page 8: PHP MySQL Workshop - facehook

PHP Programming Paradigms

Uses both procedural and object oriented

paradigms

• Procedural PHP Has been in use since the creation of PHP, its primary

paradigm. Allows for easy and quick learning of the PHP

language. Similar to other popular languages such as Visual

Basic, C++, and Fortran.

• Object Oriented PHP Similar to Java

Page 9: PHP MySQL Workshop - facehook

What is PHP Good For?• It is great for complex web page designs

E-commerce sites with heavy traffic (Amazon,Flipkart)

Secure websites (Paypal)

Email web hosts (Gmail)

Database management and search (FaceBook, naukri,

Wikipedia , Flickr )

Most content management systems such as

WordPress, Drupal, and Joomla are all based on PHP.

Page 10: PHP MySQL Workshop - facehook

PHP Introduction• PHP is known as a server-sided language. That's because

the PHP doesn't get executed on your computer, but on the

computer you requested the page from. The results are then

handed over to you, and displayed in your browser.

• PHP supports many databases ( MySQL, Informix, Oracle,

Sybase, PostgreSQL, Generic ODBC, etc.)

• It is an open source software, which is widely used and free

to download and use.

Page 11: PHP MySQL Workshop - facehook

• PHP code is interpreted by a web server with a PHP processor module which generates the resulting web page .

• PHP code is executed on the server, generating HTML which is then sent to the client. The client would receive the results of running that script, but would not know what the underlying code was.

PHP Introduction

Page 12: PHP MySQL Workshop - facehook

PHP Introduction

Page 13: PHP MySQL Workshop - facehook

Why is PHP used?1.Easy to Use

Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of "PHP mode".

<html>   <head>       <title>Brainware Baguiati PHP Example</title>   </head>   <body>       <?php        echo "Hi, I am a PHP script !";        ?>   </body></html>

Page 14: PHP MySQL Workshop - facehook

Why is PHP used?

2.Cross PlatformRuns on almost any Web server on several operating systems. One of the strongest features is the wide range of supported databases.

Web Servers: Apache, Microsoft IIS ,nGinx, etc

Operating Systems: Linux, Mac OSX, Windows

Supported Databases: IBM DB2, Informix, Ingres, MySQL, ODBC, Oracle, PostgreSQL, SQLite, Sybase

Page 15: PHP MySQL Workshop - facehook

Why is PHP used?

Software Free

Platform Free (Linux)

Development Tools Free

PHP Coder, jEdit , Notepad++

3.Cost BenefitsPHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free.

Page 16: PHP MySQL Workshop - facehook

Getting Started PHP Hello World

Above is the PHP source code.

Page 17: PHP MySQL Workshop - facehook

PHP Hello World

It renders as HTML that looks like this:

Page 18: PHP MySQL Workshop - facehook

PHP Comments

• All php files are saved with extension .php

• The can be written in notepad or any text editor

• Single line comment// this is single line comment

• Multi line comment/*….

This is a multi line comment */

Page 19: PHP MySQL Workshop - facehook

Variables Variables start with a $ symbol - $name , $age , $city

A variable name can only contain alpha-numeric

characters and underscores (A-z, 0-9, and _ )

In PHP, a variable does not need to be declared before

adding a value to it.

PHP is loosely typed language. There is no strict data

typing. PHP automatically converts the variable to the

correct data type, depending on its value.

Variables are CASE SENSITIVE.

($y and $Y are two different variables)

Page 20: PHP MySQL Workshop - facehook

PHP Conditional Statements

• if statement - use this statement to execute some code only if a specified condition is true

• if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false

• if...elseif....else statement - use this statement to select one of several blocks of code to be executed

• switch statement - use this statement to select one of many blocks of code to be executed

Page 21: PHP MySQL Workshop - facehook

Conditions• If else

if(condition) {….}

elseif(condition) {….}

else { ….}

• Switch caseswitch(var)

{

case c1: statements;break

……

Default: statements; break;

}

Page 22: PHP MySQL Workshop - facehook

PHP Arrays

• An array variable is a storage area holding a number or text. The problem is, a variable will hold only one value.

• An array is a special variable, which can store multiple values in one single variable.

Page 23: PHP MySQL Workshop - facehook

PHP Arrays

In PHP, there are three kind of arrays:

• Numeric array - An array with a numeric index

• Associative array - An array where each ID key is associated with a value

• Multidimensional array - An array containing one or more arrays

Page 24: PHP MySQL Workshop - facehook

PHP Arrays

• Numeric array - An array with a numeric index

$students_array[0] = “Rohan”;

$students_array[1] = “Dhruba”;

$students_array[2] = “Rina”;

$students_array[3] = “Tanya”;

echo "Two of my students are " . $students_array[0] . " and " . $students_array[1];

Page 25: PHP MySQL Workshop - facehook

PHP Arrays

• Associative array - An array where each ID key is associated with a value

$marks[“Rohan”] = 90; $marks[“Dhruba”] = 80; $marks[“Rina”] = 65; $marks[“Tanya”] = 88;

echo "Rohan has secured : " . $marks["Rohan"] . "<br />"; echo "Dhruba has secured :" . $marks["Dhruba"] . "<br />"; echo "Rina has secured : " . $marks["Rina"] . "<br />";echo "Tanya has secured :" . $marks["Tanya"];

Page 26: PHP MySQL Workshop - facehook

PHP Arrays

• Multidimensional array - An array containing one or more arrays

$shop = array( array("rose", 1.25 , 15),                array("daisy", 0.75 , 25),                array("orchid", 1.15 , 7)              ); 

echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";

echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]."<br />";

echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]."<br />";

Page 27: PHP MySQL Workshop - facehook

PHP Loops

• while - loops through a block of code while a specified condition is true• do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true• for - loops through a block of code a specified number of times• foreach - loops through a block of code for each element in an array

Page 28: PHP MySQL Workshop - facehook

Loops• for

for(intialisation;condition;increment/decrement)

{ //do this code }• while

while ( conditional statement is true ) {

//do this code;

} • do While

do {….} while(conditional statement is true);a do-while loop first do's and secondly checks the while condition!

• foreach

$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {

echo "$value <br>";

}

Page 29: PHP MySQL Workshop - facehook

Operators are used to operate on values.There are four classifications of operators:

Arithmetic

Assignment

Comparison

Logical

Operators

Page 30: PHP MySQL Workshop - facehook

PHP Operators

Page 31: PHP MySQL Workshop - facehook

PHP Operators

Page 32: PHP MySQL Workshop - facehook

PHP Operators

Page 33: PHP MySQL Workshop - facehook

PHP Operators

Page 34: PHP MySQL Workshop - facehook

Functions and Parameters

• PHP functions need to be defined with key word function

• It can have zero or more values (parameters)

• Functions may or may not return values

• If a function need to return value, the last statement of the function should be return– return value;

Page 35: PHP MySQL Workshop - facehook

Functions

• Parameter less function<?php

function sayHi()

{

echo “hi”;

}

?>

This can be called as <?php sayHi(); ?>

in the program

Page 36: PHP MySQL Workshop - facehook

• Parameterized function<?php

function greet($name)

{

echo “Hello “ . $name;

}

?>

This can be called <?php greet(‘Sachin’);?>

This gives an output Hello Ram

Functions

Page 37: PHP MySQL Workshop - facehook

Functions

• Function returning value<?php

function add($a,$b)

{

return ($a + $b);

}

?>

When called like <?php echo add(1,2);?>

we will get an output 3 in the browser.

Page 38: PHP MySQL Workshop - facehook

ExampleThe following example shows how to display content according to the day of the week. 0 (for Sunday) through 6 (for Saturday)

<?php

$today_dayofweek = date (“w”);

if ($today_dayofweek == 0) { echo “enjoy….. today is Sunday !”; }else { echo “Go to Work…today is not Sunday.”;}?>

Page 39: PHP MySQL Workshop - facehook

Complete Example• Step 1: Universal header and footer in a single file

• Create a file called header.php. This file will have all of theheader HTML code. Remember to remove the closing </BODY> and </HTML> tags.

<html><head>

<title>Brainware PHP Workshop</title><link rel="stylesheet" type="text/css“ href=“mycssfile.css">

</head><body>

<table width=80% border=0>

<tr><td> <div align=center> Welcome to BRAINWARE </div></td></tr>

</table>

Page 40: PHP MySQL Workshop - facehook

• Step 2: Universal header and footer in a single file• Next, create a file called footer.php. This file will have all

of the footer HTML code

Complete Example

<table width=80% border=0>

<tr><td>

<div align=center> Brainware Baguiati Campus<br/>

<a href=mailto:[email protected]>Send Us Email</a>

</div>

</td></tr>

</table>

</body>

</html>

Page 41: PHP MySQL Workshop - facehook

• Step 3: Universal header and footer in a single file• This is the basic template that you will use on all of the pages. Make sure you

name the files with a .php extension so that the server will process the PHP code. In this example, we assume the header and footer files are located in the same directory.

<?php

// header

include("header.php");

?>

Insert content here!

<?php

// footer

include("footer.php");

?>

Complete Example

Page 42: PHP MySQL Workshop - facehook

Example ( Benefits )Any changes to header or footer only require editing of a single file. These

changes will take effect in all the pages where header or footer are

included. This reduces the amount of work necessary for site maintenance

and redesign. Helps separate the content and design for easier maintenance

Page 1Content

Page 5Content

Page 3Content

Page 2Content

Page 4Content

Header

Footer

Page 43: PHP MySQL Workshop - facehook

PHP Forms - $_GET Function The built-in $_GET function is used to collect values from a form sent with method=“GET”

<form method= "get" action="action.php">... <input …….</form>

GET is better for non-secure data, like query strings in Google

Information sent from a form with the GET method is visible to everyone

all variable names and values are displayed in the URL

action.php?name=rahul&age=23&qual=btech

This method should not be used when sending passwords or other sensitive information!

Page 44: PHP MySQL Workshop - facehook

• The built-in $_POST function is used to collect values from a form sent with method=“POST".

<form method="post" action=“action.php">... <input …….</form>

• Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

• Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).

PHP Forms - $_POST Function

Page 45: PHP MySQL Workshop - facehook

PHP Database Handling

• PHP can connect to– MySQL– Access and other databases

like Oracle, postgre sql, SQL Server etc

• There are separate methods available for connecting to the databases.

Page 46: PHP MySQL Workshop - facehook

What is MySQL• MySQL is a database system used on the web that

runs on a server

• MySQL is ideal for both small and large applications

• MySQL is very fast, reliable, and easy to use

• MySQL supports standard SQL

• MySQL compiles on a number of platforms

• MySQL is free to download and use

• MySQL is developed, distributed, and supported by Oracle Corporation

Page 47: PHP MySQL Workshop - facehook

Companies using MySQL

Page 48: PHP MySQL Workshop - facehook

MySQL Connection

• mysql_connect(dbserver,userid,password)

is used for connecting to MySQL dbserver using a userid and password

• $con = mysql_connect(‘localhost’,’root’,’123456’);

This gets a connection to the localhost mysql server using the credentials root and 123456

If the mysql server cannot be connected, it will throw an error stating the problem.

Page 49: PHP MySQL Workshop - facehook

MySQL Select DB• mysql_select_db(database,connection)

• mysql_select_db(“students”,$con);– This will select the database students under

the server localhost– If unable to select the database, an error will

be thrown

Parameter Description

database Required. Specifies the database to use.

connection Optional. Specifies the MySQL connection.

Page 50: PHP MySQL Workshop - facehook

MySQL Execute Querymysql_query(sql query, connection);

This will execute the sql statement on the database and store the result in a variable.

Example:

$rs = mysql_query(“select * from students”, $con);$row = mysql_fetch_array($rs);– This will fetch a row from the table and store in $row– Data Values can be accessed like - $row[“age”] – returns value of column age from the database in the fetched row.– $agevar = $row[“age”]

Parameter Description

database Required. Specifies the SQL query to run.

connection Optional. Specifies the MySQL connection.

Page 51: PHP MySQL Workshop - facehook

Our Task

Page 52: PHP MySQL Workshop - facehook

Registration and Login

• We will develop a registration and login system using PHP and MySQL

• We will need two forms, php scripts and a database

• Database - name “pailan”• Table – name “mst_facehook”• Two pages – “registration.html” & “login.html”• PHP Script – “registration.php” & “login.php”• That’s it……… Lets start……

Page 53: PHP MySQL Workshop - facehook

Registration and Login• Database - name “pailan”• Table – name “mst_facehook”

Field Typeid int(10)email varchar(200)password varchar(100)gender char(1)city varchar(20)

Page 54: PHP MySQL Workshop - facehook

Registration Page

Page 55: PHP MySQL Workshop - facehook

Login Page

Page 56: PHP MySQL Workshop - facehook
Page 57: PHP MySQL Workshop - facehook

SHOUBHIK ROY / SANDEEP BAJORIAemail : [email protected] : +91 98319 28362 / +91 98309 36993web : www.brainware-baguiati.com www.facebook.com/Brainware.Kolkata


Recommended