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

Tutorial: Basant Namdeo Iips, Davv

php mysql

Uploaded by

Madhvi Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Tutorial: Basant Namdeo Iips, Davv

php mysql

Uploaded by

Madhvi Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Tutorial

Basant Namdeo
IIPS, DAVV
 Itgives basic knowledge of PHP.
 Gives some Example and understanding of the
Server side programming.
 PHP stands for ‘PHP Hypertext Preprocessor’
 It is Open-source, Server-side scripting language
 Used to generate dynamic web-pages
 PHP code is written between reserved PHP tags.
 There can be both HTML and php code in the same file.
This file is saved by file extension .php.
 Itis a Interpreted language. PHP scripts are parsed by
PHP interpreter(or PHP module in Web server) at run-
time when the page is requested by client browser.
 It
is Executed on the server-side and the resulted
HTML contents are send to the client.
 That’s why the Source-code not visible by client
 ‘View Source’ in browsers does not display the PHP code
 Itis integrated with a number of popular databases,
including MySQL, PostgreSQL,Oracle, Sybase,
Informix , Microsoft SQL Server and many more.
 PHP performs system functions, i.e. from files on a
system(web server) it can create, open , read , write
and close them.
 PHP can handle forms , i.e. gather form data from
HTML form elements, save data to a file or process
that data to anyway you want.
 You can add, delete, modify elements within your
database thru PHP.
 Access cookies variables and set cookies.
 Using PHP, you can restrict user to access some pages
of your website.
 Structurally it is similar to C/C++.
 Supports procedural and object-oriented paradigm (to some
degree).
 All PHP statements end with a semi-colon.
 Each PHP script must be enclosed in the reserved PHP tag.
 This script block can be used any no. of times as and when
required with other HTML tags.

<?php
…Here Comes PHP Code….
?>
 Comment Symbols just like C, C++, and shell comment
symbols

// Single line Comment As in C++ and Java

# Single line comment As in Unix Shell

/* Multi line comment


…………………………….
These can span multiple lines */
 All PHP variables must begin with a “$” sign
 PHP is Case-sensitive, so($Age != $age != $AGE)
 Variable have no special data type. If we store the
number in variable then it work as number variable
and if we store string then it works as string
variable.
 Global and locally-scoped variables
 Global variables can be used anywhere
 Local variables restricted to a function or class
 Certain variable names reserved by PHP
 Form variables ($_POST, $_GET)
 Server variables ($_SERVER)
 Etc.
<?php
$age = 25; // Numerical variable
$message = “Hello”; // String variable

$age = ($age * 2); // Multiplies age by 2


$message = ($message * 2); // Invalid expression
?>
 It is method, which send the output to the client’s
browser window. It basically echoes what is passed in
its arguments. This method first evaluate the
expression or put the variables value.

<?php
$age = 35; // Numerical variable
$name = “Basant”; // String variable

echo $name // Outputs Basant


echo $age,$name; // Outputs 35Basant
echo “My Name is $name and age is $age”;
// Outputs My Name is Basant and my age is 35
echo ‘Name=$name’; // Outputs Name=$name (See Single
// quote …..)
?>
 Usea period to join strings into one. (print
function is also works like echo.)

<?php
$string1=“Hello”;
$string2=“PHP”;
$string3=$string1 . “ ” . $string2;
print $string3;
?>

Hello PHP
 Ifthe string has a set of double quotation marks
that must remain visible, use the \ [backslash]
before the quotation marks to ignore and display
them.

<?php
$heading=“\”Computer Science\””;
print $heading;
?>

“Computer Science”
PHP Control Structures
PHP also has many control structure as other programming
language have. There are if-else, switch case. There various types
of loops are also available for example for loop, foreach, while, do-
while etc.

Switch case
Switch($var)
{
case “val1”:
statement;
break;
case “val2”:
statement;
break;
default:
statement;
}
 if (condition)
<?php
{
if($user==“IIPS”)
Statements; {
} print “Hello IIPS.”;
}
elseif(condition) else
{ {
print “You are not IIPS.”;
Statement;
}
} ?>
else
{
}
 while (condition)
{
Statements;
}
 for (intialize;condition; increment/decrement)
{ statements; <?php
} $count=0;
while($count<3)
{
print “hello PHP. ”;
$count += 1;
// $count = $count + 1;
// or
// $count++;
?>
2009/4/1 $datedisplay=date(“yyyy/m/d”);
print $datedisplay;
# If the date is April 1st, 2009
# It would display as 2009/4/1

$datedisplay=date(“l, F m, Y”);
Wednesday, April 1, 2009 print $datedisplay;
# If the date is April 1st, 2009
# Wednesday, April 1, 2009
M Jan
F January
m 01
n 1

Day of Month d 01
Day of Month J 1
Day of Week l Monday
Day of Week D Mon
 Functions MUST be defined before then can be
called
 Function headers are of the format
function functionName($arg_1, $arg_2, …, $arg_n)

 Note that no return type is specified


 Unlike variables, function names are not case
sensitive (foo(…) == Foo(…) == FoO(…))
<?php
// This is a function
function foo($arg_1, $arg_2)
{
$arg_2 = $arg_1 * $arg_2;
return $arg_2;
}

$result_1 = foo(12, 3); // Store the function


echo $result_1; // Outputs 36
echo foo(12, 3); // Outputs 36
?>
include “opendb.php”;
include “closedb.php”;
This inserts files; the code in files will be inserted into
current code. This will provide useful and protective means once
you connect to a database, as well as for other repeated
functions.

Include (“footer.php”);

The file footer.php might look like:

<hr SIZE=11 NOSHADE WIDTH=“100%”>


<i>Copyright © 2008-2010 IIPS </i><br>
<i>ALL RIGHTS RESERVED</i></font><br>
<i>URL: http://www.iips.edu</i></font><br>
PHP - Forms
•Access to the HTTP POST and GET data is simple in PHP
•The global variables $_POST[] and $_GET[] contain the
request data
<?php
if ($_POST["submit"])
echo "<h2>You clicked Submit!</h2>";
elseif ($_POST["cancel"])
echo "<h2>You clicked Cancel!</h2>";
?>
<form action="form.php" method="post">
<input type="submit" name="submit" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</form>
WHY PHP – Sessions ?

A PHP session variable is used to store information about, or


change settings for a user session. Session variables hold information
about one single user, and are available to all pages in one application.
Above task can also be achieved by Cookies (a text file storing
key/value pair type information on user’s or say client computer.). But the
problem with cookies are, first they are stored on local computer and
user does not want to store any type of information on his/her computer
and second because the are simple text files, they can be easily read by
any text editor software and other user of the system. That’s why we
can say cookies are unreliable.
PHP has a great set of functions that can achieve the same
results of Cookies and more without storing information on the user's
computer. PHP Sessions store the information on the web server in a
location that you chose in special files. These files are connected to the
user's web browser via the server and a special ID called a "Session ID".
This is nearly 99% flawless in operation and it is virtually invisible to the
user.
PHP - Sessions

•Sessions store their identifier in a cookie in the client’s browser


•Every page that uses session data must be proceeded by the
session_start() function
•Session variables are then set and retrieved by accessing the global
$_SESSION[]

•Save it as session.php
<?php
session_start();
if (!$_SESSION["count"])
$_SESSION["count"] = 0;
if ($_GET["count"] == "yes")
$_SESSION["count"] = $_SESSION["count"] + 1;
echo "<h1>".$_SESSION["count"]."</h1>";
?>
<a href="session.php?count=yes">Click here to count</a>
Destroy PHP - Sessions
Destroying a Session
why it is necessary to destroy a session when the session will get destroyed when
the user closes their browser. Well, imagine that you had a session registered called
"access_granted" and you were using that to determine if the user was logged into
your site based upon a username and password. Anytime you have a login feature,
to make the users feel better, you should have a logout feature as well. That's
where this cool function called session_destroy() comes in handy. session_destroy()
will completely demolish your session (no, the computer won't blow up or self
destruct) but it just deletes the session files and clears any trace of that session.
NOTE: If you are using the $_SESSION superglobal array, you must clear the array
values first, then run session_destroy.
Here's how we use session_destroy():
Destroy PHP - Sessions

<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION = array();
session_destroy();
echo "<strong>Step 5 - Destroy This Session </strong><br />";
if($_SESSION['name']){
echo "The session is still active";
} else {
echo "Ok, the session is no longer active! <br />";
echo "<a href=\"page1.php\"><< Go Back Step 1</a>";
}
?>
PHP Overview
 Easy learning
 Syntax Perl- and C-like syntax. Relatively easy
to learn.
 Large function library
 Embedded directly into HTML
 Interpreted, no need to compile
 Open Source server-side scripting language
designed specifically for the web.
 Conceived in 1994, now used on +10 million
web sites.
 Outputs not only HTML but can output XML,
images (JPG & PNG), PDF files and even
Flash movies all generated on the fly. Can
write these files to the file system.
 Supports a wide-range of databases
(20+ODBC).
 PHP also has support for talking to other
services using protocols such as LDAP, IMAP,
SNMP, NNTP, POP3, HTTP.
First PHP script
 Save as sample.php:
<!– sample.php -->
<html>
<body>
<strong>Hello World!</strong><br />
<?php
echo “<h2>Hello, World</h2>”;
?>
<?php
$myvar = "Hello World";
echo $myvar;
?>
</body>
</html>
 Function: List content of table.

 showtable.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
Student List is :</br>
<?php
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = '';
$dbname = 'test';
$tableName = "studmast";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());
if (!mysql_select_db($dbname))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$tableName}");
if (!$result)
die("Query to show fields from table failed!" . mysql_error());
/*Creating Table Header*/
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$tableName}</h1>";
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
/*End of Table Header Section */
/*Creating Table Rows.......*/
while($row = mysql_fetch_array($result) )
{
echo "<tr>";
/*
echo "<td>{$row['stuid']}</td>";
echo "<td>{$row['stuname']}</td>";
echo "<td>{$row['cgpa']}</td>";
*/
echo "<td>".$row['stuid']."</td>";
echo "<td>".$row['stuname']."</td>";
echo "<td>".$row['cgpa']."</td>";
echo "</tr>\n";
}
mysql_free_result($result);
mysql_close($conn);
?>
</body>
</html>
 mysql_connect()
 mysql_select_db()
 include()
 mysql_query()
 mysql_num_rows()
 mysql_fetch_array()
 mysql_close()
 PHP began in 1995 when Rasmus Lerdorf developed
a Perl/CGI script toolset he called the Personal
Home Page or PHP
 PHP 2 released 1997 (PHP now stands for Hypertex
Processor). Lerdorf developed it further, using C
instead
 PHP3 released in 1998 (50,000 users)
 PHP4 released in 2000 (3.6 million domains).
Considered debut of functional language and
including Perl parsing, with other major features
 PHP5.0.0 released July 13, 2004 (113 libraries>1,000
functions with extensive object-oriented
programming)
 PHP5.0.5 released Sept. 6, 2005 for maintenance
and bug fixes

You might also like