PHP Lab Final Programs
PHP Lab Final Programs
PART-A
1. Create a PHP program to find odd or even number from given number.
<?php
$a = 11;
if ($a % 2==0)
echo "$a is EVEN”;
else
echo "$a is ODD";
?>
Output:
1
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$a = 11;
$b = 4;
$c = 31;
if($a > $b)
{
if($a > $c)
echo "Largest number = $a";
else
echo "Largest number = $c";
}
else
{
if($c > $b)
echo "Largest number = $c"; else
echo "Largest number = $b";
}
?>
Output:
2
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$a = 10;
$b = 25;
echo "Before Swapping"."<br>"."a = $a"."<br>"."b = $b"."<br>";
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "After Swapping"."<br>"." a = $a"."<br>"."b = $b";
?>
Output:
3
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$no = 5;
$value = 13.5;
$name = "Sam";
$var = true;
$myarray= array(110,45," Sam",1.5,true);
echo gettype($no)."<br/>";
echo gettype($value)."<br/>";
echo gettype($name)."<br/>";
echo gettype($var)."<br/>";
echo gettype($myarray)."<br/>";
?>
Output:
4
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$number;
$name = "Sam";
$val=10.5;
var_dump(isset($number));
var_dump(isset($val));
var_dump(isset($name));
?>
Output:
5
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$var = '7.423';
$int_value = intval($var);
echo $int_value ."<br>";
$var = 0x423;
$int_value = intval($var);
echo $int_value."<br>";
$var = "64";
echo intval($var)."\n".intval($var, 8)."<br>";
?>
Output:
6
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$str1 = 'a';
$str2 = 'a';
echo strcmp($str1, $str2)."<br/>";
$str3 = 'BCA';
$str4 = 'bca';
echo strcmp($str3, $str4)."<br/>";
$str5 = "Sam";
$str6 = "sam";
echo strcmp($str5, $str6)."<br/>";
?>
Output:
7
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<!DOCTYPE html>
<head>
<title>PHP Form Demo</title>
</head>
<body>
<h2>Form Demo</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<h3>Submitted Data:</h3>";
echo "Name: " . htmlspecialchars($_POST['name']) . "<br>";
echo "Email: " . htmlspecialchars($_POST['email']) . "<br>";
echo "Gender: " . htmlspecialchars($_POST['gender']) . "<br>";
echo "Message: " . htmlspecialchars($_POST['message']) . "<br>";
}
?>
<form method="POST">
<input type="text" name="name" placeholder="Name" required><br><br>
<input type="email" name="email" placeholder="Email" required><br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="Male" required> Male
<input type="radio" name="gender" value="Female" required> Female<br><br>
<textarea name="message" placeholder="Message" rows="3"
required></textarea><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Output:
8
SHARNBASVA UNIVERSITY, BCA (CO-ED)
9
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<html>
<head>
<title>Session</title>
</head>
<body>
<?php
session_start();
$_SESSION["name"]="Sam";
$_SESSION["message"]="Welcome to PHP world!";
echo "Welcome ".$_SESSION["name"]."<br/>";
echo "Message is:".$_SESSION["message"];
?>
</body>
</html>
Output:
10
SHARNBASVA UNIVERSITY, BCA (CO-ED)
PART-B
10. Write a PHP program to create a database using MySQL.
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$sql="CREATE DATABASE BCA5th";
mysqli_query($conn,$sql);
if (!$conn)
{
die("Connection failed: ");
}
echo "Connected & Created Database successfully";
?>
Output:
11
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
$sql="DROP DATABASE BCA5th";
mysqli_query($conn,$sql);
if (!$conn)
{
die("Connection failed: ");
}
echo "Connected & Dropped Database successfully";
?>
Output:
12
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<html>
<head>
<title>Create Table </title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "BCA5th";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE Items
(ItemNo INT(6) ,
Mouse VARCHAR(30) ,
Keyboard VARCHAR(30) ,
Speaker VARCHAR(50))";
if ($conn->query($sql) === TRUE) {
echo "Table Created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
</body>
</html>
Output:
13
SHARNBASVA UNIVERSITY, BCA (CO-ED)
14
SHARNBASVA UNIVERSITY, BCA (CO-ED)
13. Write a PHP program to insert record into a table using MySQL.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "BCA5th";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to insert values into table
$sql = "insert into Items(ItemNo,Mouse,Keyboard,Speaker)
values (104,'Samsung', 'Lenovo', 'Creative')";
if ($conn->query($sql) === TRUE) {
echo "Values Inserted Successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
Output:
15
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "BCA5th";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to Update values into table
$sql = "update Items set Mouse='Samsung' where ItemNo=103";
if ($conn->query($sql) === TRUE) {
echo "Updated Successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
Output:
16
SHARNBASVA UNIVERSITY, BCA (CO-ED)
15. Write a PHP program to select data and show into table format.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bca5th";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT *from items";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<br> ItemNo: ". $row["ItemNo"]. " - Mouse: ". $row["Mouse"]. " - Keyboard:" .
$row["Keyboard"] . " - Speaker: ". $row["Speaker"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Output:
17
SHARNBASVA UNIVERSITY, BCA (CO-ED)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "BCA5th";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to Drop table
$sql = "drop table Items";
if ($conn->query($sql) === TRUE) {
echo "Dropped Table Successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
Output:
18
SHARNBASVA UNIVERSITY, BCA (CO-ED)
17. Create a student Registration in PHP and Save and Display the student Records.
<!DOCTYPE html>
<html>
<head>
<title>Student Registration</title>
</head>
<body>
<h2>Student Registration</h2>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"
method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="gender">Gender:</label>
<select id="gender" name="gender" required>
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br><br>
<label for="address">Address:</label>
<textarea id="address" name="address" required></textarea><br><br>
<h2>Student Records</h2>
<?php
// Database connection
$conn = mysqli_connect("localhost", "root", "", "bca5th");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
19
SHARNBASVA UNIVERSITY, BCA (CO-ED)
if (mysqli_query($conn, $sql)) {
echo "Student registered successfully!<br>";
} else {
echo "Error: " . mysqli_error($conn);
}
}
if (mysqli_num_rows($result) > 0) {
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Roll No</th>
<th>Gender</th>
<th>Address</th>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['roll_no']}</td>
<td>{$row['gender']}</td>
<td>{$row['address']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No records found.";
}
20
SHARNBASVA UNIVERSITY, BCA (CO-ED)
Output:
21
SHARNBASVA UNIVERSITY, BCA (CO-ED)
18. Write a PHP program to Develop student registration form and display all the submitted
data on another page.
18a.html
<html>
<head>
<title>insert data in form</title>
</head>
<body>
<form action = "18b.php" method = "POST"> Name:
<input type = "text" name = "txtname"><br><br>
Roll no.:<input type = "text" name = "txtr_no"><br><br>
Address:<textarea name = "add" type = "textarea"></textarea><br><br>
Contact No:<input type = "text" name = "txtc_no"><br><br>
Email ID:<input type = "text" name = "txteid"><br><br>
<input type = "Submit" name = "insert" value = "Save">
<input type = "Reset" value = "Cancle">
</form>
</body>
</html>
18b.php
<html>
<head>
<title>get data from another page</title>
</head>
<body>
<?php
echo "Name:" .$_POST["txtname"]."</br>";
echo "Roll no.:".$_POST["txtr_no"]."</br>";
echo "Address:".$_POST["add"]."</br>";
echo "Contact No.:".$_POST["txtc_no"]."</br>";
echo "Email ID:".$_POST["txteid"]."</br>";
?>
</body>
</html>
22
SHARNBASVA UNIVERSITY, BCA (CO-ED)
Output:
23
SHARNBASVA UNIVERSITY, BCA (CO-ED)
19. Write a PHP program to read customer information like c_no, c_name, item_purchased
and mob_no from customer table and display all this information in table format on output
screen.
<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$database = "shop_db"; // Replace with your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
24
SHARNBASVA UNIVERSITY, BCA (CO-ED)
Output:
25
SHARNBASVA UNIVERSITY, BCA (CO-ED)
20. Write a PHP program that keeps track of how many times a visitor has loaded the page.
<?php
session_start(); if(isset($_SESSION['counter']))
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['counter'] = 1;
}
echo "You have visited this page ".$_SESSION['counter']." time in this session";
?>
Output:
26