PHP Note
PHP Note
PHP is a powerful tool for making dynamic and interactive Web pages and it is similar to the ASP. PHP is
executed on the server hence we call it to server side scripting Language. PHP supports many databases such
as MySQL, Informix, Oracle etc. PHP is open source software and it is free to download from the Internet.
PHP runs on different platforms such as Windows, Linux, Unix, etc. Install Apache (or IIS) on your own
server, install PHP, and MySQL (Install WAMP or LAMP Server)
PHP code is executed on the server, and the plain HTML result is sent to the browser.
<html><body>
<?php
echo "Hello World";
?>
</body></html>
Comments in PHP
<html><body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body></html>
Variables in PHP
$var_name = value;
Example 1 Example 2
<?php <?php
$txt="Hello World!"; $txt="Hello World";
$x=16; echo $txt;
?> ?>
Arithmetic Operators
Assignment Operators
Comparison Operators
if Statement
<html>
<body>
Syntax
<?php
if (condition) code to be executed if condition is true; $d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
Example else
echo "Have a nice day!";
?>
</body>
While Loops </html>
Syntax
while (condition)
{
code to be executed;
}
Example
<html><body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body></html>
Syntax
for (init; condition; increment)
{
code to be executed;
}
Example
<html><body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body></html>
<html>
<head>
<title>Variables - Some Practice</title>
</head>
<body>
<?php
print("It Worked!<br>");
echo ("Hello World!<br>");
$x=6.6;
$y=6;
$fname='kuma';
$lname='Livera';
echo($x+$y);
print("<br>".$x*$y);print("<br>".$x/$y);
print("<br>".$fname." ".$lname);
?>
</body>
</html>
Example 2 :
Input your information in one web page and display them in another web page.
<html>
<head>
<title>Data Entry Form</title>
</head>
<body>
detailForm.php Output
<html>
<head>
<title>User Details Form</title>
<?PHP
$name = $_POST['myname'];
$age = $_POST['age'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$country = $_POST['country'];
echo ("<b>"."Hello! ".$name."</b>");
echo ("<br><br>"."You are ".$age." years old");
echo ("<br><br>"."Your email is ".$email);
echo ("<br><br>"."You commented our web site as : ".$comments);
echo ("<br><br>"."You are from ".$country);
?>
</head><body></body></html>
Example 3 :
Input more information in one web page and display them in another web page.
detailForm2.php Output
<html><head>
<title>User Details Form</title>
<?PHP
$fname = $_POST['fname'];$lname = $_POST['lname'];
$dob = $_POST['dob'];$gen = $_POST['gen'];
$prof = $_POST['profession'];$email = $_POST['email'];
$school = $_POST['school'];$country =
$_POST['country'];$comments = $_POST['comments'];
$h1 = $_POST['h1'];$h2 = $_POST['h2'];$h3 = $_POST['h3'];
login.html
<html>
<body>
<form action="login.php" method="post">
<table>
<tr>
<td align="center" colspan="2">
<h4>Login</h4>
</td>
</tr>
<tr>
<td align="right">
Login ID:
</td>
<td>
<input name="login" type="text" />
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input name="password" type="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Login" />
</td>
</tr>
</table>
</form>
</body>
</html>
login.php
<?php
$login=$_POST['login'];
$username="root";
$pass="";
$database="contact";
$url = "login.html";
$i=0;
while ($i < $num) {
$password=mysql_result($result,$i,"password");
if ($password == $_POST['password']) {
$url = "index.php";
session_start();
$_SESSION = array();
$_SESSION["person_id"] = mysql_result($result,$i,"id");
$_SESSION["person_name"] = mysql_result($result,$i,"name");
break;
}
$i++;
}
?>
<html>
<body>
<head>
<meta http-equiv="refresh" content="0; url=<?php echo $url;?>">
</head>
</body>
</html>
index.php
<?php
require_once("session.php");
?>
<html>
<body bgcolor="#9999FF">
Hello, <?php echo
$_SESSION["person_name"]; ?>
<br>
<table width="600">
<caption>
<h4><strong><font color="#0000CC">Address Book</font></strong></h4>
</caption>
<tr>
<td align="center"> <strong><A href="create.php">Create</A> </strong></td>
</tr>
<tr>
<td align="center"> <strong><A href="listall.php">List All</A> </strong></td>
</tr>
<tr>
<td align="center"> <strong><A href="search.php">Search</A> </strong></td>
</tr>
</table>
</form>
</body>
</html>
create.php
<?php
require_once("session.php");
?>
<html>
<body bgcolor="#66CC66">
<form action="insert.php" method="post">
<table>
<tr>
<td align="center" colspan="2"> <h4>Create a
Record</h4></td>
</tr>
<tr>
<td align="right"><strong> Login ID: </strong></td>
<td> <input name="login" type="text" /> </td>
</tr>
<tr>
<td align="right"><strong> Password: </strong></td>
Piliyandala Zonal Computer Resource Centre,
14
<td> <input name="password" type="password" /> </td>
</tr>
<tr>
<td align="right"><strong> Name: </strong></td>
<td> <input name="name" type="text" /> </td>
</tr>
<tr>
<td align="right"><strong> Comment: </strong></td>
<td> <input name="comment" type="text" /> </td>
</tr>
<tr>
<td align="right"><strong> E-Mail Address: </strong></td>
<td> <input name="mail" type="text" /> </td>
</tr>
<tr>
<td> </td>
<td> <div align="center">
<input type="submit" />
</div></td>
</tr>
</table>
</form><A href="index.php">Goto the menu</A></body></html>
insert.php
<?php
require_once("session.php");
$username="root";
$pass="";
$database="contact";
$login=$_POST['login'];
$password=$_POST['password'];
$name=$_POST['name'];
$comment=$_POST['comment'];
$mail=$_POST['mail'];
mysql_close();
?>
<html>
<body>
<h4>Record Inserted.</h4>
Piliyandala Zonal Computer Resource Centre,
15
<A href="index.php">Goto the menu</A>
</body></html>
listall.php
<?php
require_once("session.php");
?>
<html>
<body>
<table border="1" width="600">
<caption><h3>List All</h3></caption>
<tr>
<th align="center">Login ID</th>
<th align="center" width="200">Name</th>
<th align="center">Comment</th>
<th align="center">E-Mail Address</th>
<th>Delete</th>
</tr>
<?php
$username="root";
$pass="";
$database="contact";
$i=0;
//loop the all the records
while ($i < $num) {
Piliyandala Zonal Computer Resource Centre,
16
//take off all fiealds
$id=mysql_result($result,$i,"id");
$login=mysql_result($result,$i,"login");
$name=mysql_result($result,$i,"name");
$comment=mysql_result($result,$i,"comment");
$mail=mysql_result($result,$i,"mail");
//output details
echo "<tr>\n";
echo "<td><a href='edit.php?id=$id'>$login</a></td>\n"; //specify which user you edit by using ID
echo "<td>$name</td>\n";
echo "<td>$comment</td>\n";
echo "<td>$mail</td>\n";
echo "<td><a href='confirm.php?id=$id'>Delete</a></td>\n";
echo "</tr>\n";
$i++;
}
?>
</table>
<br>
<A href="index.php">Goto the menu</A>
</body>
</html>
edit.php
<?php
require_once("session.php");
?>
<html>
<body>
<form action="update.php" method="post">
<table border="1" width="600">
<caption><h3>Edit</h3></caption>
Piliyandala Zonal Computer Resource Centre,
17
<?php
$id=$_GET['id'];
$username="root";
$pass="";
$database="contact";
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$login=mysql_result($result,$i,"login");
$password=mysql_result($result,$i,"password");
$name=mysql_result($result,$i,"name");
$comment=mysql_result($result,$i,"comment");
$mail=mysql_result($result,$i,"mail");
echo "<tr>\n";
echo "<td align='right'>Login ID:</td>\n";
echo "<td><input type=\"text\" name=\"login\" value=\"$login\"/></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td align='right'>Password:</td>\n";
echo "<td><input type=\"password\" name=\"password\" value=\"$password\"/></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td align='right'>Name:</td>\n";
echo "<td><input type=\"text\" name=\"name\" value=\"$name\"/></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td align='right'>Comment:</td>\n";
echo "<td><input type=\"text\" name=\"comment\" value=\"$comment\"/></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td align='right'>E-Mail Address:</td>\n";
echo "<td><input type=\"text\" name=\"mail\" value=\"$mail\"/></td>\n";
echo "</tr>\n";
$i++;
}
?>
<tr>
<td colspan="3" align="center">
<input type='submit' value='Update' />
</td>
</tr>
</form>
</table>
<br>
<A href="index.php">Goto the menu</A>
</body></html>
update.php
<?php
require_once("session.php");
$username="root";
$pass="";
$database="contact";
$id=$_POST['id'];
$login=$_POST['login'];
$password=$_POST['password'];
$name=$_POST['name'];
$comment=$_POST['comment'];
$mail=$_POST['mail'];
mysql_close();
?>
<html>
<body>
<h4>Record updated.</h4>
<A href="index.php">Goto the menu</A>
</body>
</html>
<?php
$id=$_POST['id'];
$username="root";
$pass="";
$database="contact";
</form>
<html>
<body>
<h4>Record deleted.</h4>
<A href="index.php">Goto the menu</A>
</body>
</html>
confirm.php
<?php
require_once("session.php");
?>
<html><body>
<form action="delete.php" method="post">
<table border="1" width="600">
<caption><h3>Is it OK to delete the following record?</h3></caption>
<tr>
<th align="center">Login ID</th>
<th align="center">Name</th>
<th align="center">Comment</th>
<th align="center">E-Mail Address</th>
</tr>
Piliyandala Zonal Computer Resource Centre,
20
<?php
$id=$_GET['id'];
$username="root";
$pass="";
$database="contact";
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$login=mysql_result($result,$i,"login");
$name=mysql_result($result,$i,"name");
$comment=mysql_result($result,$i,"comment");
$mail=mysql_result($result,$i,"mail");
echo "<tr>\n";
echo "<td>$login</td>\n";
echo "<td>$name</td>\n";
echo "<td>$comment</td>\n";
echo "<td>$mail</td>\n";
echo "</tr>\n";
echo "<input type=\"hidden\" name='id' value='$id'/>\n";
$i++;
}
?>
<tr>
<td colspan="4" align="center">
<input type='submit' value='Delete' />
</td>
</tr>
</form>
</table>
<br>
<A href="index.php">Goto the menu</A>
</body></html>
<?php
require_once("se
ssion.php");
?>
<html><body>
<form action="search.php" method="post">
<table border="1" width="800">
<caption><h3>Search</h3></caption>
<tr>
<td align="right">Name:</td>
<?php
$key=$_POST['key'];
echo "<td><input type=\"text\" name=\"key\" value=\"$key\"/></td>\n"
?>
</tr>
<tr>
<td colspan="2" align="center">
<input type='submit' value='Search' />
</td>
</tr>
</table>
</form>
<?php
$id=$_POST['id'];
$username="root";
$pass="";
$database="contact";
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$login=mysql_result($result,$i,"login");
$name=mysql_result($result,$i,"name");
$comment=mysql_result($result,$i,"comment");
$mail=mysql_result($result,$i,"mail");
echo "<tr>\n";
echo "<td><a href='edit.php?id=$id'>$login</a></td>\n";
echo "<td>$name</td>\n";
echo "<td>$comment</td>\n";
echo "<td>$mail</td>\n";
echo "<td><a href='confirm.php?id=$id'>Delete</a></td>\n";
echo "</tr>\n";
$i++;
}
?>
</table>
<br>
<A href="index.php">Goto the menu</A>
</body>
</html>
session.php
<?php
session_start();
// Check if session exists
if (!isset($_SESSION['person_id'])) {
header("Location:login.html");
exit();
}
?>
MySQL
RDBMS
Friends Table
Field names
TINYINT − A very small integer that can be signed or unsigned. If signed, the allowable
range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can
specify a width of up to 4 digits.
SMALLINT − A small integer that can be signed or unsigned. If signed, the allowable
range is from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You
can specify a width of up to 5 digits.
BIGINT − A large integer that can be signed or unsigned. If signed, the allowable range is
from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable range
is from 0 to 18446744073709551615. You can specify a width of up to 20 digits.
FLOAT(M,D) − A floating-point number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D). This is not required and will default to
10,2, where 2 is the number of decimals and 10 is the total number of digits (including
decimals). Decimal precision can go to 24 places for a FLOAT.
String Types
CHAR(M) − A fixed-length string between 1 and 255 characters in length (for example
CHAR(5)), right-padded with spaces to the specified length when stored. Defining a
length is not required, but the default is 1.
BLOB or TEXT − A field with a maximum length of 65535 characters. BLOBs are
"Binary Large Objects" and are used to store large amounts of binary data, such as
images or other types of files. Fields defined as TEXT also hold large amounts of data.
The difference between the two is that the sorts and comparisons on the stored data
are case sensitive on BLOBs and are not case sensitive in TEXT fields. You do not
specify a length with BLOB or TEXT.
ENUM − An enumeration, which is a fancy term for list. When defining an ENUM, you
are creating a list of items from which the value must be selected (or it can be NULL).
For example, if you wanted your field to contain "A" or "B" or "C", you would define
your ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate
that field.
MY SQL – commands
mysql>show databases;
mysql>create table person (id int not null auto_increment, login varchar(30), password varchar(15), name
varchar(30), comment varchar(255), email varchar(30), primary key (id));
mysql>show tables;
mysql>describe person;
mysql>insert into person values (0, ‘lal’, ‘123lal’, ‘Lalani Abenayaka’, ‘This web site is useful’,
‘[email protected]’);
Remove database
mysql>drop database contact;