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

PHP Lab Programs V Sem

The document provides examples of PHP programs and scripts for various tasks like displaying text, dates, and Fibonacci series. It also includes examples of connecting to a MySQL database, inserting, updating, deleting and fetching rows from tables, and using cookies to pass variables between pages. The examples cover basic to more advanced PHP and MySQL functions and operations.

Uploaded by

hafsaadn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
361 views

PHP Lab Programs V Sem

The document provides examples of PHP programs and scripts for various tasks like displaying text, dates, and Fibonacci series. It also includes examples of connecting to a MySQL database, inserting, updating, deleting and fetching rows from tables, and using cookies to pass variables between pages. The examples cover basic to more advanced PHP and MySQL functions and operations.

Uploaded by

hafsaadn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Write a PHP program to Display “Hello”

<html>
<head>
<title>name</title>
</head>
<body>
<?php
echo "<b><i>Hello</b> </i>";
?>
</body>
</html>

Output:
2. Write a PHP Program to display the today’s date.

<html>
<head>
<title>name</title>
</head>
<body>
<?php
$currentDateTime = date('Y-m-d, H:i:s');
echo "Today's date and time is:" .$currentDateTime;
?>
</body>
</html>

Output:
3. Write a PHP program to display Fibonacci series.
<html>
<head>
<title>Fibonacci series</title>
</head>
<body>
<?php
$n = 0;
$a = 0;
$b = 1;
echo "Fibonacci series is: ";
echo "$a, $b";
while ($n < 5 )
{
$c = $b + $a;
echo ", ";
echo "$c";
$a = $b;
$b = $c;
$n = $n + 1;
}
?>
</body>
</html>

Output:
4. Write PHP script to demonstrate passing variables with cookies.
<html>
<head>
<title>Cookie Demo</title>
</head>
<body>
<form method="post" action="cookies.php">
Enter Your Name :
<input type="text" name="fname" value="<?php echo $_COOKIE['myname']; ?>">
<input type="submit" value="Create Cookie" name="submit">
</form>
</body>
</html>

Output:

<?php
if(isset($_POST['submit']))
{
$name=$_POST['fname'];
setcookie('myname',$name,time()+100,"/","",0);
echo "Your name is : ".$_COOKIE['myname'];
if(!isset($_COOKIE['myname']))
{
echo "failed to create cookie";
}
}
?>

Output:
5. Write a program to keep track of how many times a visitor has loaded the page.
<html>
<head>
<title>visitors count</title>
</head>
<body>
<?php
$handle = fopen("counter.txt", "r"); //create a counter.txt file first
if(!$handle)
{
echo "could not open the file" ;
}
else
{
$counter = (int ) fread($handle,20);
fclose ($handle);
$counter++;
echo" <strong> you are visitor no ". $counter . " </strong> " ;
$handle = fopen("counter.txt", "w" );
fwrite($handle,$counter);
fclose($handle);
}
?>
</body>
</html>

Output:
6. Write a PHP application to add new Rows in a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p>&nbsp;</p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="reset" value="reset">
</div></td></tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>
Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$eid = $_POST['eid'];
$ena = $_POST['ena'];
$age = $_POST['age'];
$sal = $_POST['sal'];
$validate = "insert into `employee` (eid,ena,age,sal)values('$eid','$ena','$age','$sal')";
$query_validate = mysqli_query($conn,$validate);
if($query_validate)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Inserted successfully</b></center>";
}
}
?>

Output:
7. Write a PHP application to modify the Rows in a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p>&nbsp;</p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="rubmit" value="reset">
</div></td></tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>

Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$db_eid = $_POST['eid'];
$db_sal= $_POST['sal'];
$update = "update `employee` set sal='$db_sal' where eid='$db_eid'";
$query_update = mysqli_query($conn,$update);
if($query_update)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Updated successfully</b></center>";
}
}
?>

Output:
8. Write a PHP application to delete the Rows from a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p>&nbsp;</p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="reset" value="reset">
</div></td></tr>
</table>
<p>&nbsp;</p>
<label></label>
<label></label>
</form>
</body>
</html>

Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$db_eid = $_POST['eid'];
$delete = "delete from `employee` where eid='$db_eid'";
$query_delete = mysqli_query($conn,$delete);
if($query_delete)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Deleted successfully</b></center>";
}
}
?>

Output:
9. Write a PHP application to fetch the Rows in a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p>&nbsp;</p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="reset" value="reset">
</div></td></tr>
</table>
<p>&nbsp;</p>
<label></label>
<label></label>
</form>
</body>
</html>

Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$db_eid = $_POST['eid'];
$db_sal= $_POST['sal'];
$update = "update `employee` set sal='$db_sal' where eid='$db_eid'";
$query_update = mysqli_query($conn,$update);
if($query_update)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Updated successfully</b></center>";
}
}
?>

Output:
10. Write a PHP script to connect MySQL server from your website.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) 
{
  die("Connection failed:".mysqli_connect_error());
}
echo "Connected successfully";
?>

Output:

11. Write a program to edit name of customer to “Kiran” with cust-no =1, and to delete record with cust-
no=3.

a) Create table customer ( cno number, cna varchar2(10), res varchar2(5));


b) Insert into customer (cno,cna,res) values (1, “rajesh”, “pass”);
Insert into customer (cno,cna,res) values (2, “priya”, “pass”);
Insert into customer (cno,cna,res) values (3, “sandhya”, “pass”);
c) Update customer set cna=”kiran” where cno=1;
d) Delete * from customer where cno=3;

You might also like