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

php_ct2[2]

The document provides a comprehensive overview of various PHP and MySQL concepts, including introspection methods, features of MySQL, server roles in PHP, and data types. It also covers HTTP methods (GET and POST), session management, and database operations like Create, Read, Update, and Delete. Additionally, it includes PHP scripts for form handling, database connections, and CRUD operations.

Uploaded by

letscookbrba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

php_ct2[2]

The document provides a comprehensive overview of various PHP and MySQL concepts, including introspection methods, features of MySQL, server roles in PHP, and data types. It also covers HTTP methods (GET and POST), session management, and database operations like Create, Read, Update, and Delete. Additionally, it includes PHP scripts for form handling, database connections, and CRUD operations.

Uploaded by

letscookbrba
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

List introspection methods in detail


Ans :

2. Explain any four features of MySQL?


Ans :
1. Free to Download – MySQL is open-source and can be
downloaded for free.
2. High Performance – It works fast and handles large data
efficiently.
3. Flexibility – MySQL supports many applications and works well
with different platforms.
4. Productivity – Features like stored procedures and views help in
faster development.

3. What are the roles of server in PHP?


Ans : A server is important for running PHP-based web applications. Its
main roles include:
1. Running PHP Code – The server processes PHP scripts and
creates dynamic web pages.
2. Handling Client Requests – It receives requests from browsers
and sends back responses.
3. Connecting to Databases – The server interacts with databases
like MySQL to read or save data.
4. Managing Sessions and Cookies – It keeps track of users and
their preferences using sessions and cookies.
5. File Handling – It helps upload, read, and manage files needed
by the application.
6. Providing Security – It protects the application with
authentication, encryption, and access control.
7. Running Background Tasks – It can run scheduled tasks like
sending emails or generating reports.

4. Describe any four data types of MySQL with its size?


Ans :
5. Define the GET and POST methods
Ans : GET and POST Methods in PHP
1. GET Method:
 Sends data via the URL (visible in the address bar).
 Used for retrieving data (e.g., search queries).
 Limited data size (~2000 characters).
 Less secure as data is exposed in the URL.
2. POST Method:
 Sends data in the request body (not visible in the URL).
 Used for submitting forms, login credentials, and sensitive data.
 Allows larger data size.
 More secure than GET.
6. Difference between cookies and session.
Ans :

7. Define the uses of serialize () and unserialize().


Ans :

8. Write short note on interface with example.


Ans : An interface in PHP defines a set of methods that a class must
implement.

It does not contain any method body—only method names


(prototypes).

Interfaces are declared using the interface keyword and do not have
any variables.

 All methods in an interface are public and abstract by default.

A class uses the implements keyword to use an interface.

 PHP allows a class to implement multiple interfaces, supporting


multiple inheritance.
Example:
<?php
interface Animal {
public function makeSound();
}

class Dog implements Animal {


public function makeSound() {
echo "Woof!";
}
}

$dog = new Dog();


$dog->makeSound(); // Output: Woof!
?>

9. State the use of cloning of an object.


Ans : Cloning means making a copy of an object. It is useful when you
want to:
1. Make a Duplicate – Create a copy without changing the original.
2. Protect Original Data – Changes in the copy won’t affect the
original.
3. Save Time – No need to create a new object from the beginning.
4. Use in Design Patterns – Helpful when you need many copies of
the same object.

10. Explain introspection and serialization with example.


Ans :

11. Differentiate between Session & Cookies. (Any two points)


Ans :

12. List out the database operations.


Ans : Create, Read, Update, Delete, Alter, Truncate

13. Define MySQL.


Ans : MySQL is a popular open-source database system used to store
and manage data.
It supports standard SQL commands and works well with languages like
PHP, Java, and C++.
MySQL is known for being fast, reliable, and commonly used in web
applications.

14. How traits used in php? Explain with example.


Ans :
Traits in PHP:
 Traits allow code reuse across multiple independent classes.
 Unlike interfaces, traits can define actual method implementations.
 Traits support static methods and static properties.
 They help overcome PHP’s single inheritance limitation by enabling
multiple inheritance-like behavior.
Syntax:
class ClassName {
use TraitName;
// Class methods and properties
}
Example:
<?php
// Parent class A
class A {
public function disp1() {
echo "Parent-A <br>";
}
}

// Trait B (acts like another parent)


trait B {
public function disp2() {
echo "Parent-B <br>";
}
}

// Class C inherits A and uses Trait B


class C extends A {
use B;

public function disp3() {


echo "Child-C";
}
}

$obj = new C();


$obj->disp1(); // Output: Parent-A
$obj->disp2(); // Output: Parent-B
$obj->disp3(); // Output: Child-C
?>
Output:
Parent-A
Parent-B
Child-C

15. Write PHP script a form with 5 checkboxes (ex: programming


languages) and a submit button When selection of one/ more
checkboxes, list of selected checkboxes should be displayed
Ans :
Html : <!DOCTYPE html>
<html>
<head>
<title>Programming Languages Form</title>
</head>
<body>
<h2>Select Your Favorite Programming Languages</h2>
<form method="POST" action="one.php">
<input type="checkbox" name="languages[]" value="Python">
Python <br>
<input type="checkbox" name="languages[]" value="Java"> Java
<br>
<input type="checkbox" name="languages[]" value="JavaScript">
JavaScript <br>
<input type="checkbox" name="languages[]" value="C++"> C++
<br>
<input type="checkbox" name="languages[]" value="PHP"> PHP
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Php : <?php
if (isset($_POST['submit'])) {
if (!empty($_POST['languages'])) {
echo "<h3>You have selected:</h3>";
echo "<ul>";
foreach ($_POST['languages'] as $language) {
echo "<li>" . $language . "</li>";
}
echo "</ul>";
} else {
echo "<p style='color: red;'>Please select at least one
language.</p>";
}
}
?>

16. Write PHP script to update record in table of MySQL database.


Ans : <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$id = 1;
$newName = "Rahul Sharma";
$newEmail = "[email protected]";
$newAge = 22;

$sql = "UPDATE student SET name='$newName', email='$newEmail',


age=$newAge WHERE id=$id";

if ($conn->query($sql) === TRUE) {


echo "Record updated successfully!";
} else {
echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

17. Write PHP script to delete record in table of MySQL database.


Ans : <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$id = 1;

$sql = "DELETE FROM student WHERE id=$id";


if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully!";
} else {
echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>

18. Write a PHP program to demonstrate session management.


Ans :
Sessions store user data across multiple pages.
 session_start() initializes a session.
 Session variables are stored in $_SESSION.
 session_destroy() removes session data.
Step 1: Start and Set Session (session_start.php)
<?php
session_start(); // Start the session
$_SESSION["username"] = "JohnDoe"; // Set session variable
echo "Session started. Username is: " . $_SESSION["username"];
?>
Step 2: Access Session Data (session_get.php)
<?php
session_start(); // Start the session
if(isset($_SESSION["username"])) {
echo "Welcome, " . $_SESSION["username"];
} else {
echo "Session not set.";
}
?>
Step 3: Destroy Session (session_destroy.php)
<?php
session_start(); // Start the session
session_destroy(); // Destroy the session
echo "Session destroyed.";
?>
Output:
1. Run session_start.php → Session started. Username is: JohnDoe
2. Run session_get.php → Welcome, JohnDoe
3. Run session_destroy.php → Session destroyed.

19. Create Employee form like employee name, Address, Mobile


No., Post and Salary using different form input element and
display user inserted values in new PHP form.
Ans :
Html : <!DOCTYPE html>
<html>
<head>
<title>Employee Form</title>
</head>
<body>
<h2>Employee Details Form</h2>
<form method="POST" action="one.php">
Employee Name: <input type="text" name="name" required>
<br><br>
Address: <textarea name="address" required></textarea>
<br><br>
Mobile No.: <input type="tel" name="mobile" required>
<br><br>
Post:
<select name="post">
<option value="Manager">Manager</option>
<option value="Engineer">Engineer</option>
<option value="Developer">Developer</option>
<option value="Analyst">Analyst</option>
</select> <br><br>
Salary: <input type="number" name="salary" required>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Php : <!DOCTYPE html>
<html>
<head>
<title>Employee Details</title>
</head>
<body>
<h2>Employee Details</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<strong>Employee Name:</strong> " .
$_POST["name"] . "<br>";
echo "<strong>Address:</strong> " . nl2br($_POST["address"])
. "<br>";
echo "<strong>Mobile No.:</strong> " . $_POST["mobile"] .
"<br>";
echo "<strong>Post:</strong> " . $_POST["post"] . "<br>";
echo "<strong>Salary:</strong> $" . $_POST["salary"] .
"<br>";
} else {
echo "No data received!";
}
?>
</body>
</html>

20. Write a program to connect PHP with MySQL.


Ans : <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully to MySQL database!";
}

$conn->close();
?>

21. Write PHP script to insert record in table of MySQL database.


Ans : <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$name = "Amit Kumar";


$email = "[email protected]";
$age = 21;
$sql = "INSERT INTO student (name, email, age) VALUES ('$name',
'$email', $age)";
if ($conn->query($sql) === TRUE) {
echo "Record inserted successfully!";
} else {
echo "Error inserting record: " . $conn->error;
}

$conn->close();
?>

22. Write PHP script to select record in table of MySQL database.


Ans : <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name, email, age FROM student";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " .
$row["email"] . " - Age: " . $row["age"] . "<br>";
}
} else {
echo "No records found.";
}

$conn->close();
?>

You might also like