The document discusses PHP and MySQL for building web applications. It begins with an introduction to PHP, explaining that PHP is a widely used server-side scripting language suited for web development. It then covers PHP fundamentals like syntax, variables, data types, operators, and control structures. The document also discusses how to connect to and query MySQL databases from PHP. It provides an example of building a simple user registration and login application with PHP and MySQL to store and retrieve user data from a database table.
1. 1. Current trends – WEB Tech…
1. Current trends – WEB Tech…
2. PHP & MySQL – An Introduction
2. PHP & MySQL – An Introduction
3. Getting started
3. Getting started
4. Examples
4. Examples
5. Simple Registration & Login app
5. Simple Registration & Login app
2. Usage of server-side programming
languages for websites
PHP is now installed on more than
20 million websites and 1 million web servers.
servers
Source : http://w3techs.com
3. Usage of client-side programming
languages for websites
Source : http://w3techs.com
4. Usage of markup languages for
websites
Source : http://w3techs.com
5. 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.
7. 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.
8. 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
9. 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.
10. 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.
11. PHP Introduction
• 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.
13. 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>
14. Why is PHP used?
2.Cross Platform
Runs 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
15. Why is PHP used?
3.Cost Benefits
PHP 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.
Software
Free
Platform
Free (Linux)
Development Tools
Free
PHP Coder, jEdit , Notepad++
18. 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 */
19. 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)
20. 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
21. Conditions
• If else
if(condition) {….}
elseif(condition) {….}
else { ….}
• Switch case
switch(var)
{
case c1: statements;break
……
Default: statements; break;
}
22. 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.
23. 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
24. 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];
25. 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"];
26. 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 />";
27. 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
28. 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>";
}
29. Operators
Operators are used to operate on values.
There are four classifications of operators:
Arithmetic
Assignment
Comparison
Logical
34. 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;
35. Functions
• Parameter less function
<?php
function sayHi()
{
echo “hi”;
}
?>
This can be called as <?php sayHi(); ?>
in the program
37. 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.
38. Example
The 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.”;
}
?>
39. Complete Example
• Step 1: Universal header and footer in a single file
•
Create a file called header.php. This file will have all of the
header 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>
40. Complete Example
• 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
<table width=80% border=0>
<tr><td>
<div align=center> Brainware Baguiati Campus<br/>
<a href=mailto:info@abc.com>Send Us Email</a>
</div>
</td></tr>
</table>
</body>
</html>
41. Complete Example
•
•
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");
?>
42. 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
Header
Page 1
Content
Page 2
Content
Page 3
Content
Footer
Page 4
Content
Page 5
Content
43. 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!
44. PHP Forms - $_POST Function
• 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).
45. 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.
46. 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
48. 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.
49. MySQL Select DB
• mysql_select_db(database,connection)
Parameter
Description
database
Required. Specifies the database to use.
connection
Optional. Specifies the MySQL 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
50. MySQL Execute Query
mysql_query(sql query, connection);
This will execute the sql statement on the database and store the
result in a variable.
Parameter
Description
database
Required. Specifies the SQL query to run.
connection
Optional. Specifies the MySQL connection.
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”]
52. 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……
53. Registration and Login
• Database - name “pailan”
• Table – name “mst_facehook”
Field
id
email
password
gender
city
Type
int(10)
varchar(200)
varchar(100)
char(1)
varchar(20)
57. SHOUBHIK ROY / SANDEEP BAJORIA
email : info@brainware-baguiati.com
M : +91 98319 28362 / +91 98309 36993
web : www.brainware-baguiati.com
www.facebook.com/Brainware.Kolkata
Editor's Notes
int value can be -2147483648 these are 11 digits so default size is 11
unsigned int does not allow negative numbers so default size is 10
An INT will always be 4 bytes no matter what length is specified.
TINYINT = 1 byte
SMALLINT = 2 bytes
MEDIUMINT = 3 bytes
INT = 4 bytes
BIGINT = 8 bytes.