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

Chapter 2 PAWEB (1)

This document provides an overview of PHP sessions, including their definition, differences from cookies, and how to start and manage session variables. It also introduces database connectivity in PHP, detailing how to connect to MySQL and perform CRUD operations such as creating, reading, updating, and deleting records. Key code snippets are included to illustrate the implementation of these concepts.

Uploaded by

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

Chapter 2 PAWEB (1)

This document provides an overview of PHP sessions, including their definition, differences from cookies, and how to start and manage session variables. It also introduces database connectivity in PHP, detailing how to connect to MySQL and perform CRUD operations such as creating, reading, updating, and deleting records. Key code snippets are included to illustrate the implementation of these concepts.

Uploaded by

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

PART 2 : PHP Programming

1. Introduction to PHP Sessions


1.1 What is a Session?
Definition: Temporary storage mechanism to retain user-specific data across multiple
pages.

Difference between session and cookie:

 Storage: Session on server vs. Cookie on client


 Lifetime: Session is temporary vs. Cookie can be persistent
 Size: Session can store larger data, Cookie limited to 4KB.

1.2 Starting a PHP Session


Code for Initialization:

<?php
session_start();

Importance of session_start placement: Always at the beginning, before any HTML or


output.

1.3 Working with Session Variables


Setting session variables:
<?php
$_SESSION['username'] = 'JohnDoe';

Accessing session variables:


<?php
echo $_SESSION['username'];

Unsetting session variables:


<?php
unset($_SESSION['username']);

1.4 Destroying a Session


Ending a session:
<?php
session_destroy();

6
Difference between session_destroy() and session_unset(): session_destroy() removes
all session data, while session_unset() only clears the session data for that user.

2. Introduction to Databases in PHP


2.1 Understanding Databases
Connecting to MySQL using PHP
<?php
$conn = mysqli_connect($servername, $username, $password, $dbname);

Handling connection errors:


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

2.3 CRUD Operations in PHP


Create: Adding Data to the Database
<?php
$sql = "INSERT INTO users (firstname, lastname) VALUES ('John', 'Doe')";
$result = mysqli_query($conn, $sql);

// get the ID
$last_id = mysqli_insert_id($conn);

echo "New record created successfully. Last inserted ID is: " . $last_id;

Read: Retrieving Data from the Database


<?php
$sql = "SELECT id, firstname, lastname FROM users";

$result = mysqli_query($con, $sql);

while ($row = mysqli_fetch_assoc($result)) {


echo "id: " . $row["id"] . " - Name: " . $row["firstname"] . " " .
$row["lastname"] . "<br>";
}

Update: Modifying Data in the Database


<?php
$sql = "UPDATE users SET lastname='Smith' WHERE id=2";
$result = mysqli_query($conn, $sql);

7
Delete: Removing Data from the Database
<?php
$sql = "DELETE FROM users WHERE id=2";
$result = mysqli_query($conn, $sql);

You might also like