Date post: | 18-Nov-2014 |
Category: |
Technology |
Upload: | brainware-consultancy-pvt-ltd |
View: | 2,107 times |
Download: | 6 times |
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
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.
Usage of client-side programming languages for websites
Source : http://w3techs.com
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.
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.
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
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.
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.
• 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
PHP Introduction
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>
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
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.
Getting Started PHP Hello World
Above is the PHP source code.
PHP Hello World
It renders as HTML that looks like this:
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 */
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)
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
Conditions• If else
if(condition) {….}
elseif(condition) {….}
else { ….}
• Switch caseswitch(var)
{
case c1: statements;break
……
Default: statements; break;
}
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.
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
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];
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"];
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 />";
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
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>";
}
Operators are used to operate on values.There are four classifications of operators:
Arithmetic
Assignment
Comparison
Logical
Operators
PHP Operators
PHP Operators
PHP Operators
PHP Operators
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;
Functions
• Parameter less function<?php
function sayHi()
{
echo “hi”;
}
?>
This can be called as <?php sayHi(); ?>
in the program
• Parameterized function<?php
function greet($name)
{
echo “Hello “ . $name;
}
?>
This can be called <?php greet(‘Sachin’);?>
This gives an output Hello Ram
Functions
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.
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.”;}?>
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>
• 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>
• 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
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
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!
• 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
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.
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
Companies using MySQL
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.
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.
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.
Our Task
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……
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)
Registration Page
Login Page
SHOUBHIK ROY / SANDEEP BAJORIAemail : [email protected] : +91 98319 28362 / +91 98309 36993web : www.brainware-baguiati.com www.facebook.com/Brainware.Kolkata