PHP & MySQL – Assignment 2
BCA VI Semester | 5 Marks Each
Q1. Difference between Class and Object with suitable example [5 Marks]
A class is a blueprint or template that defines properties (variables) and behaviours
(functions). An object is a real-world instance created from that class.
Key Differences:
Point Class Object
Definition Blueprint/Template Instance of a class
Memory No memory allocated Memory allocated
Creation Defined using 'class' keyword Created using 'new' keyword
Usage Defined once Can be created many times
Example:
<?php
class Car {
public $brand;
public $color;
public function display() {
echo "Brand: " . $this->brand . ", Color: " . $this->color;
}
}
// Creating Objects
$car1 = new Car();
$car1->brand = "Toyota";
$car1->color = "Red";
$car1->display(); // Output: Brand: Toyota, Color: Red
?>
Q2. PHP Script to Implement MySQL Table Creation [5 Marks]
The following PHP script connects to MySQL and creates a table using the mysqli extension.
<?php
$conn = mysqli_connect("localhost", "root", "", "college");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE TABLE IF NOT EXISTS students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT,
email VARCHAR(100)";
if (mysqli_query($conn, $sql)) {
echo "Table created successfully!";
} else {
echo "Error: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Steps performed:
• mysqli_connect() establishes a connection to MySQL database.
• CREATE TABLE IF NOT EXISTS ensures the table is created only if it doesn't exist.
• mysqli_query() executes the SQL statement.
• mysqli_close() closes the database connection.
Q3. Any Five String Functions in PHP [5 Marks]
1. strlen() – Length of a string
<?php echo strlen("Hello World"); // Output: 11 ?>
2. strtoupper() – Convert to uppercase
<?php echo strtoupper("hello"); // Output: HELLO ?>
3. strtolower() – Convert to lowercase
<?php echo strtolower("HELLO"); // Output: hello ?>
4. strrev() – Reverse a string
<?php echo strrev("PHP"); // Output: PHP reversed: PHP ?>
5. str_replace() – Replace part of a string
<?php
echo str_replace("World", "PHP", "Hello World");
// Output: Hello PHP
?>
These functions are built-in and do not require any special library. They are commonly used
for string processing in web development.
Q4. Compare Constructor and Destructor in PHP [5 Marks]
In PHP OOP, constructor and destructor are special methods that are automatically called
during object lifecycle.
Feature Constructor Destructor
Method Name __construct() __destruct()
When Called When object is created When object is destroyed
Purpose Initialize object properties Clean up resources
Arguments Can accept parameters Cannot accept parameters
Usage Set default values, DB connect Close DB connection, free
memory
Example:
<?php
class Demo {
public function __construct() {
echo "Object Created!\n";
}
public function __destruct() {
echo "Object Destroyed!\n";
}
}
$obj = new Demo(); // Output: Object Created!
// When script ends: Object Destroyed!
?>
Q5. Advantages of Using phpMyAdmin [5 Marks]
phpMyAdmin is a free web-based GUI tool used to manage MySQL databases without
writing SQL commands manually.
Advantages:
• User-Friendly Interface: Easy to create, modify, and delete databases/tables using
forms and menus.
• No SQL Knowledge Required: You can perform most database operations without
writing SQL queries.
• Import/Export: Supports importing and exporting data in formats like SQL, CSV, XML,
and Excel.
• Database Management: Easily create, rename, drop databases and manage user
permissions.
• Query Execution: Has a built-in SQL editor to run custom queries and see results
instantly.
phpMyAdmin is widely used in local development environments like XAMPP and WAMP,
making it ideal for beginners learning database management.
Q6. MySQL Client – Definition and Benefits [5 Marks]
What is a MySQL Client?
A MySQL Client is a software tool or application that allows a user to communicate with the
MySQL database server. It sends SQL commands to the server and displays the results.
Examples include the MySQL Command Line Client, phpMyAdmin, MySQL Workbench, and
HeidiSQL.
Benefits of MySQL Client:
• Direct Interaction: Allows users to directly send SQL queries to the database server.
• Portability: Clients can connect to remote MySQL servers over a network using
hostname and port.
• Multiple Interfaces: Available as command-line (mysql shell) or GUI tools for ease of
use.
• Administrative Control: Used to manage users, grant permissions, create and drop
databases.
• Debugging: Helps developers test and debug SQL queries interactively.
Example (MySQL Command Line):
mysql -u root -p
USE college;
SHOW TABLES;
Q7. Deleting MySQL Table Records Using PHP [5 Marks]
The DELETE SQL statement is used to remove records from a table. In PHP, this is done
using mysqli_query().
Syntax:
DELETE FROM table_name WHERE condition;
PHP Script Example:
<?php
$conn = mysqli_connect("localhost", "root", "", "college");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Delete student with id = 3
$sql = "DELETE FROM students WHERE id = 3";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully!";
} else {
echo "Error: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Important Notes:
• Always use WHERE clause in DELETE to avoid deleting all records.
• DELETE without WHERE removes all rows from the table.
• Use mysqli_affected_rows() to check how many rows were deleted.
Q8. Types of Inheritance in PHP [5 Marks]
Inheritance allows one class (child) to inherit properties and methods from another class
(parent). PHP supports the following types:
1. Single Inheritance
One child class inherits from one parent class.
<?php
class Animal { public function eat() { echo 'Eating'; } }
class Dog extends Animal { public function bark() { echo 'Barking'; }
}
$d = new Dog(); $d->eat(); $d->bark();
?>
2. Multilevel Inheritance
A class inherits from a child class (chain of inheritance).
class A { ... } class B extends A { ... } class C extends B { ... }
3. Hierarchical Inheritance
Multiple child classes inherit from the same parent class.
class Animal { ... }
class Dog extends Animal { ... }
class Cat extends Animal { ... }
4. Interface-based (Multiple-like) Inheritance
PHP does not support multiple inheritance directly. However, a class can implement multiple
interfaces to achieve similar behaviour.
interface A { public function hello(); }
interface B { public function world(); }
class C implements A, B {
public function hello() { echo 'Hello'; }
public function world() { echo 'World'; }
}
Note: PHP supports only single class inheritance but multiple interface implementation.
Q9. Any Five Date and Time Functions in PHP [5 Marks]
1. date() – Format current date/time
<?php echo date("d-m-Y"); // Output: 02-05-2026 ?>
2. time() – Get current Unix timestamp
<?php echo time(); // Output: 1746144000 (seconds since Jan 1 1970) ?
>
3. mktime() – Create timestamp for a specific date
<?php echo mktime(0, 0, 0, 12, 25, 2025); // Timestamp for Dec 25,
2025 ?>
4. strtotime() – Convert a date string to a timestamp
<?php echo strtotime("next Monday"); // Timestamp for next Monday ?>
5. date_diff() – Difference between two dates
<?php
$date1 = new DateTime("2025-01-01");
$date2 = new DateTime("2026-01-01");
$diff = date_diff($date1, $date2);
echo $diff->days . " days difference"; // Output: 365 days difference
?>
Q10. Function with Default Arguments in PHP [5 Marks]
In PHP, you can assign default values to function parameters. If the caller does not pass a
value, the default is used automatically.
Syntax:
function functionName(param1, param2 = defaultValue) {
// function body
}
Example 1 – Simple default argument:
<?php
function greet($name, $msg = "Welcome") {
echo "$msg, $name!\n";
}
greet("Nandeesh"); // Output: Welcome, Nandeesh!
greet("Ravi", "Hello"); // Output: Hello, Ravi!
?>
Example 2 – Default value in calculation:
<?php
function calculateTax($amount, $rate = 18) {
echo "Tax = " . ($amount * $rate / 100) . "\n";
}
calculateTax(1000); // Tax = 180 (uses default 18%)
calculateTax(1000, 12); // Tax = 120 (uses given 12%)
?>
Rules for Default Arguments:
• Default arguments must be placed at the end of the parameter list.
• You can have multiple default parameters.
• Default values must be constant expressions (not variables).
End of Assignment