0% found this document useful (0 votes)
3 views26 pages

PHP Lab Final Programs

The document contains a series of PHP programming exercises aimed at BCA students at Sharnbasva University. It includes tasks such as determining odd/even numbers, finding the maximum of three numbers, swapping two numbers, and demonstrating various PHP functions. Additionally, it covers database operations using MySQL, including creating, dropping, and manipulating tables, as well as handling form submissions for student registration.

Uploaded by

kulkarnivineet01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views26 pages

PHP Lab Final Programs

The document contains a series of PHP programming exercises aimed at BCA students at Sharnbasva University. It includes tasks such as determining odd/even numbers, finding the maximum of three numbers, swapping two numbers, and demonstrating various PHP functions. Additionally, it covers database operations using MySQL, including creating, dropping, and manipulating tables, as well as handling form submissions for student registration.

Uploaded by

kulkarnivineet01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 26

SHARNBASVA UNIVERSITY, BCA (CO-ED)

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)

2. Write a PHP program to find the maximum of three numbers.

<?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)

3. Write a PHP program to swap two numbers.

<?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)

4. Write a PHP Program to demonstrate the variable function: gettype()

<?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)

5. Write a PHP Program to demonstrate the variable function: isset()

<?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)

6. Give the example of variable function: intval()

<?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)

7. Give the example of string function: strcmp()

<?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)

8. Write a PHP program that demonstrate form element (input elements).

<!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)

9. Write a program that demonstrate use of session

<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)

11. Write a PHP program to drop a database using MySQL.

<?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)

12. Write a PHP program to create a table in MySQL.

<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)

14. Write a PHP program to update 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 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)

16. Write a PHP program to drop 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 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="roll_no">Roll No.:</label>


<input type="number" id="roll_no" name="roll_no" 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>

<input type="submit" name="register" value="Register">


</form>

<h2>Student Records</h2>

<?php
// Database connection
$conn = mysqli_connect("localhost", "root", "", "bca5th");

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Handle form submission


if (isset($_POST['register'])) {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$roll_no = intval($_POST['roll_no']);
$gender = mysqli_real_escape_string($conn, $_POST['gender']);

19
SHARNBASVA UNIVERSITY, BCA (CO-ED)

$address = mysqli_real_escape_string($conn, $_POST['address']);

$sql = "INSERT INTO students (name, roll_no, gender, address) VALUES


('$name', $roll_no, '$gender', '$address')";

if (mysqli_query($conn, $sql)) {
echo "Student registered successfully!<br>";
} else {
echo "Error: " . mysqli_error($conn);
}
}

// Fetch and display student records


$result = mysqli_query($conn, "SELECT * FROM students");

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.";
}

// Close the database connection


mysqli_close($conn);
?>
</body>
</html>

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);
}

// SQL query to fetch customer information


$sql = "SELECT * FROM customer";
$result = $conn->query($sql);

// Check if there are records in the result


if ($result->num_rows > 0) {
// Display customer information in table format
echo "<table border='1' cellspacing='0' cellpadding='10'>";
echo "<tr>
<th>Customer No</th>
<th>Customer Name</th>
<th>Item Purchased</th>
<th>Mobile Number</th>
</tr>";

// Fetch rows and display them


while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row['c_no'] . "</td>
<td>" . $row['c_name'] . "</td>
<td>" . $row['item_purchased'] . "</td>
<td>" . $row['mob_no'] . "</td>
</tr>";
}
echo "</table>";
} else {
echo "No customer records found.";

24
SHARNBASVA UNIVERSITY, BCA (CO-ED)

// Close the connection


$conn->close();
?>

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

You might also like