Install WAMP Server and PHP Basics
Install WAMP Server and PHP Basics
Practical No.1
AIM- Write the steps to install Wamp server on your PC
Solution: Steps to install WAMP Server on your PC:
Page-1
DWD LAB FILE BCA SEM - III E1 01521102024
4. Test WAMP Server:
○ Open a web browser and go to localhost or [Link].
○ You should see the WAMP Server homepage.
5. Create a PHP Test File :
○ Navigate to C:\wamp\www\ and create a file called [Link].
○ Add <?php phpinfo(); ?> and save it.
○ Go to localhost/[Link] in your browser to see the PHP info page.
6. Start Developing Websites:
○ Create a folder in C:\wamp\www\ for your project.
○ Access your website by going to localhost/yourprojectname in the browser
Page-2
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.2
AIM- Write a PHP Script to get the PHP Version
Solution:
<?php
$php_version = phpversion();
echo "Current PHP Version: " . $php_version;
?>
OUTPUT:
Page-3
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.3
AIM- Write a PHP Script to display configuration information by using the function
phpinfo().
Solution:
<?php
phpinfo();
?>
OUTPUT:
Page-4
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.4
AIM- Create a PHP Page and print hello everyone write steps of execution
Solution:
<?php
echo "Hello Everyone";
?>
1. Create a new file named [Link].
2. Add this code inside [Link]:
3. Place [Link] in your web server’s document root folder (e.g., htdocs or www).
4. Start your web server (Apache, Nginx, etc.).
5. Open a web browser and navigate to [Link]
6. View the output: Hello Everyone displayed in the browser.
OUTPUT:
Page-5
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.5
AIM- Write a PHP Script to display the following:
LEARN PHP
STUDY PHP at IITM
I’m about to learn PHP!
This string was made with multiple parameters
Solution:
<?php
// Display the required messages using echo
echo "LEARN PHP<br>";
echo "STUDY PHP at IITM<br>";
echo "I’m about to learn PHP!<br>";
echo "This string was made with multiple parameters";
?>
OUTPUT:
Page-6
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.6
AIM: Write a script using a variable "$whatisit" to print the following to the browser. Your
echo statements may include no words except "Value is". Value is String, Value is
Integer, Value is Boolean, Value is None.
Solution:
<?php
$whatisit = "String.";
echo "Value is " . $whatisit;
?>
OUTPUT:
Page-7
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.7
Solution:
<?php
define("WELCOME_MESSAGE", "Welcome to PHP [Link]!!!");
define("WELCOME_MESSAGE_INSENSITIVE", "Welcome to [Link]!!!", true);
echo WELCOME_MESSAGE . "<br>";
echo WELCOME_MESSAGE_INSENSITIVE . "<br>";
?>
OUTPUT:
Page-8
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.8
AIM- PHP Script to Find the Area of a Triangle
Solution:
<?php
$base = 10;
$height = 5;
$area = 0.5 * $base * $height;
echo "The area of the triangle is: " . $area . " square units.";
?>
OUTPUT:
Page-9
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.9
AIM- Write a script using one variable "Sumit" to print the following to the browser.
Convert or set the data type using settype() function for the following:
Value is string.
Value is double.
Value is Boolean.
Value is integer.
Solution:
<?php
$Sumit = "Hello, this is a string";
echo "Value is string: " . $Sumit . "<br>";
settype($Sumit, "double");
echo "Value is double: " . $Sumit . "<br>";
settype($Sumit, "boolean");
echo "Value is Boolean: " . ($Sumit ? "true" : "false") . "<br>";
settype($Sumit, "integer");
echo "Value is integer: " . $Sumit . "<br>";
$Sumit = "123";
settype($Sumit, "integer");
echo "Converted from string to integer: " . $Sumit . "<br>";
$Sumit = 12.34;
settype($Sumit, "string");
echo "Converted from double to string: " . $Sumit . "<br>";
$Sumit = true;
settype($Sumit, "double");
echo "Converted from boolean to double: " . $Sumit . "<br>";
$Sumit = 10;
settype($Sumit, "boolean");
echo "Converted from integer to boolean: " . ($Sumit ? "true" : "false") . "<br>";
?>
OUTPUT:
Page-10
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.10
AIM- Program to find whether the number is even or odd
Solution:
<?php
$number = 10;
if ($number % 2 == 0) {
echo "$number is an even number.";
} else {
echo "$number is an odd number.";
}
?>
OUTPUT:
Page-11
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.11
AIM- Write a program to display whether a number is prime or not
Solution:
<?php
function is_prime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}
if (is_prime($num)) {
echo "$num is a prime number.\n";
} else {
echo "$num is not a prime number.\n";
}
?>
OUTPUT:
Page-12
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.12
AIM- Write a program to check out a number is leap or not
Solution:
<?php
function is_leap_year($year) {
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
return true;
}
return false;
}
if (is_leap_year($year)) {
echo "$year is a leap year.\n";
} else {
echo "$year is not a leap year.\n";
}
?>
OUTPUT:
Page-13
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.13
AIM- Program to swap two values of numbers using 3 variables
Solution:
<?php
function swap_values(&$a, &$b) {
$temp = $a;
$a = $b;
$b = $temp;
}
$x = 10;
$y = 11;
swap_values($x, $y);
OUTPUT:
Page-14
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.14
AIM- Write a program to check largest number between two numbers using if else
statements and conditional operators
Solution:
● If else
Solution:
<?php
function find_largest($a, $b) {
if ($a > $b) {
return $a;
} elseif ($b > $a) {
return $b;
} else {
return "Both are equal";
}
}
$num1 = 10;
$num2 = 15;
● Conditional Operator
Solution:
<?php
$num1 = 10;
$num2 = 15;
OUTPUT:
Page-15
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.15
AIM- WAP to print the Factorial of a number.
Solution:
<?php
function factorial($n) {
if ($n == 0 || $n == 1) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
$num = 5;
OUTPUT:
Page-16
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.16
AIM- Program to Display the Sum of Integers from 1 to n
Solution:
<?php
$n = 10;
$sum = 0;
OUTPUT:
Page-17
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.17
AIM- Create an Index Array Using Both Methods and Display All Values with and
Without Index Value.
Solution:
<?php
$array1 = array(10, 20, 30, 40, 50);
$array2[] = 10;
$array2[] = 20;
$array2[] = 30;
$array2[] = 40;
$array2[] = 50;
Page-18
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.18
AIM- Create an Index Array and Find the Sum of Its Elements
Solution:
<?php
$array = array(10, 20, 30, 40, 50);
$sum = array_sum($array);
OUTPUT:
Page-19
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.19
AIM- Create an associative array:
● Count the array element and display all values of the array using for each loop.
● Create a 2D associative array and display its element with each loop.
● Find the following:
○ Sum of two matrices.
○ Difference between two matrices.
○ Product of two matrices.
o Transpose of a matrix.
Solution:
<?php
$array = array("a" => 10, "b" => 20, "c" => 30);
echo "Number of elements: " . count($array) . "\n";
$array2D = array(
"row1" => array("a" => 1, "b" => 2),
"row2" => array("c" => 3, "d" => 4)
);
$matrix1 = array(
array(1, 2),
array(3, 4)
);
$matrix2 = array(
array(5, 6),
array(7, 8)
);
echo "\nSum of matrices:\n";
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
echo $matrix1[$i][$j] + $matrix2[$i][$j] . " ";
Page-20
DWD LAB FILE BCA SEM - III E1 01521102024
}
echo "\n";
}
Page-21
DWD LAB FILE BCA SEM - III E1 01521102024
OUTPUT:
Page-22
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.20
Solution: <?php
$matrix1 = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9)
);
$matrix2 = array(
array(9, 8, 7),
array(6, 5, 4),
array(3, 2, 1)
);
function display_matrix($matrix) {
$result = array();
return $result;
$result = array();
return $result;
$result = array();
$result[$i][$j] = 0;
return $result;
function transpose_matrix($matrix) {
Page-24
DWD LAB FILE BCA SEM - III E1 01521102024
$result = array();
$result[$i][$j] = $matrix[$j][$i];
return $result;
display_matrix($matrix1);
display_matrix($matrix2);
display_matrix($sum);
display_matrix($difference);
display_matrix($product);
$transpose1 = transpose_matrix($matrix1);
display_matrix($transpose1);
$transpose2 = transpose_matrix($matrix2);
Page-25
DWD LAB FILE BCA SEM - III E1 01521102024
display_matrix($transpose2);
?>
OUTPUT:
Page-26
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.21
Solution:
<?php
$array = array("apple" => 2, "banana" => 3, "orange" => 1, "mango" => 4);
asort($array);
print_r($array);
ksort($array);
print_r($array);
arsort($array);
print_r($array);
krsort($array);
print_r($array);
print_r(array_keys($array));
Page-27
DWD LAB FILE BCA SEM - III E1 01521102024
print_r(array_values($array));
?>
OUTPUT:
Page-28
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.22
AIM- Write a program in PHP to demonstrate the use of following functions: arrrat_pad(),
array_slice(), array_splice(), list().
Solution:
<?php
// 1. array_pad()
$input = [1, 2, 3];
$paddedArray = array_pad($input, 5, 0); // Pads to length 5 with 0
echo "array_pad():\n";
print_r($paddedArray);
// 2. array_slice()
$fruits = ["apple", "banana", "cherry", "date", "fig"];
$slicedArray = array_slice($fruits, 1, 3); // Get 3 elements starting from index 1
echo "\narray_slice():\n";
print_r($slicedArray);
// 3. array_splice()
$numbers = [10, 20, 30, 40, 50];
$removed = array_splice($numbers, 2, 2, [99, 100]);
// Remove 2 elements starting at index 2 (30, 40) and replace with [99, 100]
echo "\narray_splice():\n";
echo "Removed elements: ";
print_r($removed);
echo "Modified array: ";
print_r($numbers);
// 4. list()
$data = ["John", "Doe", 30];
list($firstName, $lastName, $age) = $data;
echo "\nlist():\n";
echo "First Name: $firstName\n";
echo "Last Name: $lastName\n";
echo "Age: $age\n";
?>
Page-29
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.23
Aim- Write a function to reverse a string.
Solution:
<?php
function rev($str) {
$reversed = '';
// Loop through the string from the end to the beginning
for ($i = strlen($str) - 1; $i >= 0; $i--) {
$reversed .= $str[$i];
}
return $reversed;
}
OUTPUT:
Page-30
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.24
Aim- Write a php function that checks whether a passed string is palindrome or not.
Solution:
<?php
function isPalindrome($string) {
$reversed = strrev($string);
isPalindrome("Madam");
isPalindrome("sir");
isPalindrome("MOM");
?>
OUTPUT:
Page-31
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.25
Aim- Write a PHP script:
i. Transform a string to all uppercase letters.
ii. Transform a string to all lowercase letters.
iii. Make a string’s first character uppercase.
Make a string’s first character of all the words uppercase.
Solution:
<?php
$string = "welcome to php programming";
$uppercase = strtoupper($string);
$lowercase = strtolower($string);
$firstCharUpper = ucfirst($string);
$allWordsUpper = ucwords($string);
Page-32
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.26
Aim- Write a program in PHP to perform sorting on an array using a user-defined function.
Solution:
<?php
function sortArray($arr) {
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
}
return $arr;
}
$sorted = sortArray($numbers);
Page-33
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.27
Aim- Write a program in PHP to demonstrate the use of
(i) The $_GET variables
(ii) The $_POST variables
(iii) $_REQUEST variables
Solution:
[Link]
<!DOCTYPE html>
<html>
<head>
<title>GET, POST and REQUEST Example</title>
</head>
<body>
</body>
</html>
[Link]
<?php
echo "Using \$_GET Variables<br><br>";
$name = $_GET['name'];
$age = $_GET['age'];
Page-34
DWD LAB FILE BCA SEM - III E1 01521102024
[Link]
<?php
echo "Using \$_POST Variables<br><br>";
$name = $_POST['name'];
$age = $_POST['age'];
[Link]
<?php
echo "Using \$_REQUEST Variables<br><br>";
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
Page-35
DWD LAB FILE BCA SEM - III E1 01521102024
Page-36
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.28
Aim- Write a program in PHP to
(i) Create a file
(ii) Read and Display the contents of previously created file
(iii) Modify the contents of an existing file
Solution:
<?php
$filename = "[Link]";
$file = fopen($filename, "w");
echo "<br>File modified successfully. Check the file for updated contents.";
?>
OUTPUT:
Page-37
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.29
Aim- Create a form in PHP to upload and display images.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Image Upload and Display</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" required>
<input type="submit" name="upload" value="Upload">
</form>
<?php
if(isset($_POST['upload'])){
$target_dir = "uploads/";
if(!is_dir($target_dir)){
mkdir($target_dir);
}
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$allowed_types = array("jpg","jpeg","png","gif");
if(in_array($file_type, $allowed_types)){
if(move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)){
echo "<h3>Image Uploaded Successfully</h3>";
echo "<img src='$target_file' width='300'>";
} else {
echo "Error uploading file.";
}
} else {
echo "Only JPG, JPEG, PNG & GIF files are allowed.";
}
}
?>
</body>
</html>
OUTPUT:
Page-38
DWD LAB FILE BCA SEM - III E1 01521102024
Page-39
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.30
Aim- Write a program to create a cookie. Read cookie value, and delete a cookie.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Cookie Example</title>
</head>
<body>
<?php
if(isset($_POST['create'])){
setcookie("username", $_POST['username'], time()+3600);
echo "Cookie created successfully.";
}
if(isset($_POST['read'])){
if(isset($_COOKIE['username'])){
echo "Cookie Value: " . $_COOKIE['username'];
} else {
echo "No cookie found.";
}
}
if(isset($_POST['delete'])){
setcookie("username", "", time()-3600);
echo "Cookie deleted successfully.";
}
?>
<form method="post">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" name="create" value="Create Cookie">
<input type="submit" name="read" value="Read Cookie">
<input type="submit" name="delete" value="Delete Cookie">
</form>
</body>
</html>
OUTPUT:
Page-40
DWD LAB FILE BCA SEM - III E1 01521102024
Page-41
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.31
Aim- Write a program in PHP to
(i) Start a PHP Session
(ii) Store Session variables
(iii) Destroy a session
Solution:
<html>
<head>
<title>Session Example</title>
</head>
<body>
<?php
session_start();
if(isset($_POST['store'])){
$_SESSION['user'] = $_POST['username'];
echo "Session variable stored successfully.";
}
if(isset($_POST['display'])){
if(isset($_SESSION['user'])){
echo "Session Value: " . $_SESSION['user'];
} else {
echo "No session found.";
}
}
if(isset($_POST['destroy'])){
session_unset();
session_destroy();
echo "Session destroyed successfully.";
}
?>
<form method="post">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" name="store" value="Store Session">
<input type="submit" name="display" value="Display Session">
<input type="submit" name="destroy" value="Destroy Session">
</form>
</body>
</html>
OUTPUT:
Page-42
DWD LAB FILE BCA SEM - III E1 01521102024
Page-43
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.32
Aim- Create Admin Login and Logout forms using Session variables.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Admin Login System</title>
</head>
<body>
<?php
session_start();
$admin_user = "admin";
$admin_pass = "12345";
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];
if($username == $admin_user && $password == $admin_pass){
$_SESSION['admin'] = $username;
echo "<h3>Login Successful. Welcome, $username!</h3>";
echo '<form method="post"><input type="submit" name="logout" value="Logout"></form>';
} else {
echo "<h3>Invalid Credentials</h3>";
}
} elseif(isset($_POST['logout'])){
session_unset();
session_destroy();
echo "<h3>You have been logged out.</h3>";
} elseif(isset($_SESSION['admin'])){
echo "<h3>Welcome, ".$_SESSION['admin']."!</h3>";
echo '<form method="post"><input type="submit" name="logout" value="Logout"></form>';
} else {
?>
<form method="post">
<h2>Admin Login</h2>
<input type="text" name="username" placeholder="Username" required><br><br>
<input type="password" name="password" placeholder="Password" required><br><br>
<input type="submit" name="login" value="Login">
</form>
<?php
}
?>
</body>
</html>
Page-44
DWD LAB FILE BCA SEM - III E1 01521102024
OUTPUT:
Page-45
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.33
Aim- Write a program in PHP to create a class Student. Define constructors, destructors,
and methods to display student information, percentage, and grade. Use appropriate data
members.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Student Class Example</title>
</head>
<body>
<?php
class Student {
public $name;
public $roll_no;
public $marks = array();
public $total;
public $percentage;
public $grade;
function calculatePercentage() {
$this->total = array_sum($this->marks);
$this->percentage = $this->total / count($this->marks);
}
function calculateGrade() {
if($this->percentage >= 90)
$this->grade = "A+";
elseif($this->percentage >= 75)
$this->grade = "A";
elseif($this->percentage >= 60)
$this->grade = "B";
elseif($this->percentage >= 50)
$this->grade = "C";
else
$this->grade = "Fail";
Page-46
DWD LAB FILE BCA SEM - III E1 01521102024
}
function displayInfo() {
echo "<h3>Student Information</h3>";
echo "Name: $this->name<br>";
echo "Roll No: $this->roll_no<br>";
echo "Total Marks: $this->total<br>";
echo "Percentage: $this->percentage%<br>";
echo "Grade: $this->grade<br>";
}
function __destruct() {
echo "<br>Object destroyed.";
}
}
OUTPUT:
Page-47
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.34
Aim- Write a program in PHP to create a class Person. Create another class Customer
which is a subclass of the class Person.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Inheritance Example</title>
</head>
<body>
<?php
class Person {
public $name;
public $age;
public $address;
function displayPersonInfo() {
echo "<h3>Person Details</h3>";
echo "Name: $this->name<br>";
echo "Age: $this->age<br>";
echo "Address: $this->address<br>";
}
}
function displayCustomerInfo() {
$this->displayPersonInfo();
echo "<h4>Customer Details</h4>";
echo "Customer ID: $this->customer_id<br>";
echo "Purchase Amount: ₹$this->purchase_amount<br>";
Page-48
DWD LAB FILE BCA SEM - III E1 01521102024
}
}
OUTPUT:
Page-49
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.35
Aim- Write a program in PHP to demonstrate the use of access modifiers.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Access Modifiers Example</title>
</head>
<body>
<?php
class Employee {
public $name;
private $salary;
protected $position;
echo "<br>";
OUTPUT:
Page-51
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.36
Aim- Write steps to create a database in phpMyAdmin. Create users and assign privileges.
Solution:
Step 1: Open phpMyAdmin
• Open your browser
• Type: [Link]
• Press Enter.
Step 2: Log In
• Enter username and password (usually root / your password).
Page-52
DWD LAB FILE BCA SEM - III E1 01521102024
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
Page-53
DWD LAB FILE BCA SEM - III E1 01521102024
}
$new_user = "newroot";
$new_password = "newdishita";
OUTPUT:
Page-54
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.37
AIM- Write a program in PHP to create a database table using appropriate data types and
constraints.
Solution:
<?php
$servername = "localhost";
$username = "root"; // default user for local server
$password = "dishita"; // keep blank if no password
$dbname = "dishita"; // make sure this database already exists
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
OUTPUT:
Page-55
DWD LAB FILE BCA SEM - III E1 01521102024
Page-56
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.38
AIM- Write a program in PHP to open a Connection to the MySQL Server, insert at least
10 records in a database table, display the inserted records and Close the Connection.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Insert and Display Records</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "dishita";
$dbname = "dishita";
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$records = [
['Aditi Sharma', 'BCA', 85.5],
['Rahul Mehta', 'BBA', 78.0],
['Sneha Kapoor', '[Link]', 92.3],
['Arjun Verma', 'BCA', 67.8],
['Priya Singh', 'BA', 73.4],
['Karan Patel', '[Link]', 88.1],
['Neha Jain', '[Link]', 81.7],
['Rohit Yadav', 'BCA', 69.5],
['Anjali Gupta', 'BBA', 90.2],
['Vivek Rao', '[Link]', 76.9]
];
if ($result->num_rows > 0) {
echo "<table border='1' cellpadding='5'>
<tr><th>ID</th><th>Name</th><th>Course</th><th>Marks</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['student_id']}</td>
<td>{$row['name']}</td>
<td>{$row['course']}</td>
<td>{$row['marks']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No records found.";
}
$conn->close();
echo "<br>Connection closed successfully.";
?>
</body>
</html>
OUTPUT:
Page-58
DWD LAB FILE BCA SEM - III E1 01521102024
Page-59
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.39
AIM- Write a PHP program to perform UPDATE and DELETE operations on a database
table.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Update and Delete Example</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "dishita";
$dbname = "dishita";
if(isset($_POST['update'])){
$id = $_POST['id'];
$marks = $_POST['marks'];
$sql = "UPDATE Students_PHP SET marks=$marks WHERE student_id=$id";
if($conn->query($sql))
echo "<p>Record with ID $id updated successfully.</p>";
else
echo "<p>Error updating record: " . $conn->error . "</p>";
}
if(isset($_POST['delete'])){
$id = $_POST['id'];
$sql = "DELETE FROM Students_PHP WHERE student_id=$id";
if($conn->query($sql))
echo "<p>Record with ID $id deleted successfully.</p>";
else
Page-60
DWD LAB FILE BCA SEM - III E1 01521102024
echo "<p>Error deleting record: " . $conn->error . "</p>";
}
<h3>Update Marks</h3>
<form method="post">
Enter Student ID: <input type="number" name="id" required><br><br>
Enter New Marks: <input type="number" name="marks" step="0.01" required><br><br>
<input type="submit" name="update" value="Update Record">
</form>
<h3>Delete Record</h3>
<form method="post">
Enter Student ID: <input type="number" name="id" required><br><br>
<input type="submit" name="delete" value="Delete Record">
</form>
<?php
$conn->close();
?>
</body>
</html>
OUTPUT:
Page-61
DWD LAB FILE BCA SEM - III E1 01521102024
Page-62
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.40
AIM- Write a PHP program to display records from a database table using different
conditions.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>Display Records with Conditions</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "dishita";
$dbname = "dishita";
$records = [
['Aditi Sharma', 'BCA', 85.5],
['Rahul Mehta', 'BBA', 78.0],
['Sneha Kapoor', '[Link]', 92.3],
['Arjun Verma', 'BCA', 67.8],
['Priya Singh', 'BA', 73.4],
['Karan Patel', '[Link]', 88.1],
['Neha Jain', '[Link]', 81.7],
['Rohit Yadav', 'BCA', 69.5],
['Anjali Gupta', 'BBA', 90.2],
['Vivek Rao', '[Link]', 76.9]
];
foreach ($records as $student) {
$conn->query("INSERT IGNORE INTO Students_PHP (name, course, marks)
VALUES ('$student[0]', '$student[1]', $student[2])");
}
Page-63
DWD LAB FILE BCA SEM - III E1 01521102024
if(isset($_POST['condition'])){
$condition = $_POST['condition'];
if($condition == "all"){
$sql = "SELECT * FROM Students_PHP";
} elseif($condition == "above80"){
$sql = "SELECT * FROM Students_PHP WHERE marks > 80";
} elseif($condition == "bca"){
$sql = "SELECT * FROM Students_PHP WHERE course='BCA'";
} elseif($condition == "name"){
$name = $_POST['name_search'];
$sql = "SELECT * FROM Students_PHP WHERE name LIKE '%$name%'";
}
$result = $conn->query($sql);
if($result->num_rows > 0){
echo "<h3>Filtered Records:</h3>
<table border='1' cellpadding='5'>
<tr><th>ID</th><th>Name</th><th>Course</th><th>Marks</th></tr>";
while($row = $result->fetch_assoc()){
echo "<tr>
<td>{$row['student_id']}</td>
<td>{$row['name']}</td>
<td>{$row['course']}</td>
<td>{$row['marks']}</td>
</tr>";
}
echo "</table>";
} else {
echo "<p>No records found.</p>";
}
}
?>
Page-64
DWD LAB FILE BCA SEM - III E1 01521102024
<?php
$conn->close();
?>
</body>
</html>
OUTPUT:
Page-65
DWD LAB FILE BCA SEM - III E1 01521102024
Practical No.41
AIM- Write a program in PHP to perform insert, update, delete, and select operations using
PDO.
Solution:
<!DOCTYPE html>
<html>
<head>
<title>CRUD using PDO</title>
</head>
<body>
<?php
$host = "localhost";
$dbname = "dishita";
$username = "root";
$password = "dishita";
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "<h3>Connected successfully using PDO</h3>";
if (isset($_POST['insert'])) {
$stmt = $conn->prepare("INSERT INTO Students_PHP (name, course, marks) VALUES (:name, :course,
:marks)");
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':course', $_POST['course']);
$stmt->bindParam(':marks', $_POST['marks']);
$stmt->execute();
echo "<p>Record inserted successfully.</p>";
}
if (isset($_POST['update'])) {
$stmt = $conn->prepare("UPDATE Students_PHP SET marks = :marks WHERE student_id = :id");
$stmt->bindParam(':marks', $_POST['marks']);
$stmt->bindParam(':id', $_POST['id']);
$stmt->execute();
echo "<p>Record updated successfully.</p>";
Page-66
DWD LAB FILE BCA SEM - III E1 01521102024
}
if (isset($_POST['delete'])) {
$stmt = $conn->prepare("DELETE FROM Students_PHP WHERE student_id = :id");
$stmt->bindParam(':id', $_POST['id']);
$stmt->execute();
echo "<p>Record deleted successfully.</p>";
}
if ($stmt->rowCount() > 0) {
echo "<table border='1' cellpadding='5'>
<tr><th>ID</th><th>Name</th><th>Course</th><th>Marks</th></tr>";
foreach ($result as $row) {
echo "<tr>
<td>{$row['student_id']}</td>
<td>{$row['name']}</td>
<td>{$row['course']}</td>
<td>{$row['marks']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No records found.";
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
<h3>Insert Record</h3>
<form method="post">
Name: <input type="text" name="name" required><br><br>
Course: <input type="text" name="course" required><br><br>
Marks: <input type="number" step="0.01" name="marks" required><br><br>
<input type="submit" name="insert" value="Insert Record">
</form>
<h3>Update Record</h3>
<form method="post">
Page-67
DWD LAB FILE BCA SEM - III E1 01521102024
Student ID: <input type="number" name="id" required><br><br>
New Marks: <input type="number" step="0.01" name="marks" required><br><br>
<input type="submit" name="update" value="Update Record">
</form>
<h3>Delete Record</h3>
<form method="post">
Student ID: <input type="number" name="id" required><br><br>
<input type="submit" name="delete" value="Delete Record">
</form>
</body>
</html>
OUTPUT:
Page-68
DWD LAB FILE BCA SEM - III E1 01521102024
Page-69