PHP UNIT 3 and 4
PHP UNIT 3 and 4
UNIT 3
Here are the detailed notes for Unit III as per your syllabus:
2. Directory:
A directory (or folder) is a container that holds files or other directories,
helping organize data in a hierarchical structure.
Example in PHP:
"r" : Read
2. Closing a File:
Closing a file releases the resources and ensures data integrity.
Example in PHP:
fclose($file);
Example in PHP:
copy("source.txt", "destination.txt");
2. Renaming a File:
Changes the name of a file.
Example in PHP:
rename("oldname.txt", "newname.txt");
3. Deleting a File:
Removes a file from the directory.
Example in PHP:
unlink("filename.txt");
Example in PHP:
2. Deleting a Directory:
Example in PHP:
rmdir("folder_name");
Example in PHP:
$files = scandir("folder_name");
foreach ($files as $file) {
echo $file . "<br>";
}
move_uploaded_file($_FILES["file"]["tmp_name"], "upload
s/" . $_FILES["file"]["name"]);
2. File Downloading:
Allows users to download files from the server.
Example in PHP:
2. Session Functionality:
Starting a Session:
session_start();
$_SESSION["username"] = "JohnDoe";
echo $_SESSION["username"];
Destroying a Session:
session_destroy();
What is a Cookie?
A cookie is a small piece of data stored on the user's browser by the web
server. It is used for maintaining stateful information across browsing
sessions.
Example uses:
Parameters:
2. Accessing a Cookie:
echo $_COOKIE["user"];
3. Deleting a Cookie:
These notes are detailed and suitable for both understanding and directly writing
in exams. Let me know if you'd like further clarification or move to the next topic!
UNIT 4
Here are detailed and easy-to-understand notes for Unit IV:
Introduction to RDBMS
Example:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Example:
try {
$conn = new PDO("mysql:host=localhost;dbname=my_dat
abase", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE
_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
1. INSERT Operation
Adds new rows to a table.
Example:
2. DELETE Operation
Removes rows from a table.
Example:
3. UPDATE Operation
Modifies existing rows.
Example:
4. SELECT Operation
Retrieves data from a table.
Example:
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"] . " - Name: " . $row["nam
e"] . "<br>";
}
} else {
echo "0 results";
}
Executing Queries
Example:
Handling Errors:
Example:
if ($conn->error) {
echo "Error: " . $conn->error;
}
Joins in SQL
1. Cross Join
Example:
2. Inner Join
Returns rows with matching values in both tables.
Example:
3. Outer Joins
Left Outer Join: Returns all rows from the left table and matching rows from
the right table.
Right Outer Join: Returns all rows from the right table and matching rows from
the left table.
4. Self Join
Joins a table with itself.
Example:
These notes provide a comprehensive explanation of the topics in Unit IV. Let me
know if you'd like further details or assistance with another unit!