0% found this document useful (0 votes)
29 views26 pages

Database Connectivity and Session Handling

nill

Uploaded by

Leeroy Mugadza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views26 pages

Database Connectivity and Session Handling

nill

Uploaded by

Leeroy Mugadza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Advanced Web Technology

09CE1503

Database Connectivity and


Session Handling

1
PHP – MySQL database
connectivity
Key Components of MySQL Database Connectivity
1. Connection: Establishing a link between the application and the
MySQL server.
2. Authentication: Using credentials (username and password) to
authenticate the connection.
3. Query Execution: Sending SQL commands to the database to perform
various operations.
4. Result Handling: Processing the data returned from the database.
5. Error Handling: Managing any errors that occur during the database
operations.
6. Connection Closure: Properly closing the connection once operations
are completed.
PHP – MySQL database
Connectivity using MySQLi
● MySQLi (MySQL Improved) is an extension provided by PHP
to interact with MySQL databases.
● It offers both procedural and object-oriented approaches.
● You can create a connection using the mysqli_connect
function in procedural style or the mysqli class in object-
oriented style.
PHP – MySQL database
connectivity
● MySQL database connectivity refers to the process of
establishing a connection between a software application and a
MySQL database.
● This connection allows the application to interact with the
database, enabling it to perform various operations such as
querying, inserting, updating, and deleting data.
● In the context of PHP, MySQL database connectivity involves
using extensions like MySQLi or PDO to facilitate
communication with the MySQL server.
1. Establishing a Connection

Procedural Style:
1. Establishing a Connection

Object-Oriented Style:
2. Selecting a Database

• Using the mysqli_select_db function (procedural) or the select_db


method (object-oriented).
Procedural Style:

Object-Oriented Style:
3. Executing Queries

• Use the mysqli_query function or the query method.


Procedural Style:

Object-Oriented Style:
4. Handling Results

• Use the mysqli_fetch_assoc function or the fetch_assoc method.


Procedural Style:

Object-Oriented Style:
5. Closing the Connection

Procedural Style:

Object-Oriented Style:
PHP – MySQL database
Connectivity using PDO
● Using PDO (PHP Data Objects) for MySQL database connectivity provides a
robust and flexible way to interact with a MySQL database.
● PDO supports various database systems and offers features like prepared
statements, which enhance security and performance.

Steps for MySQL Database Connectivity Using PDO


1. Create a Connection: Establish a connection to the MySQL database using
PDO.
2. Prepare and Execute Queries: Prepare SQL statements and execute
them.
3. Handle Results: Fetch and process the results from executed queries.
4. Close the Connection: PDO connections are automatically closed when
the object is destroyed.
PHP – MySQL database
Connectivity using PDO
1. Create a Connection:
○ new PDO("mysql:host=$servername;dbname=$dbname",
$username, $password)
○ This creates a new PDO instance and connects to the MySQL
database.
○ setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCE
PTION)
○ Sets the error mode to exception, which helps in catching and
handling errors.
PHP – MySQL database
Connectivity using PDO
2. Prepare and Execute Queries:
○ $stmt = $conn->prepare("SELECT id, firstname, lastname
FROM MyGuests")
○ Prepares an SQL statement for execution.
○ $stmt->execute() Executes the prepared statement.
3. Handle Results:
○ $stmt->setFetchMode(PDO::FETCH_ASSOC) Sets the fetch
mode to associative array.
○ $results = $stmt->fetchAll() Fetches all results from the
executed query.
○ foreach($results as $row) Loops through the results and
processes each row.
PHP – MySQL database
Connectivity using PDO
4. Close the Connection:
○ $conn = null Sets the connection to null, which closes the
connection.
Creating Tables
Retrieving Tables
Concept of Session

● A session is a way to store information (variables) to be used across


multiple pages during a user's visit to a website.
● It allows the server to store user-specific data temporarily, typically in
memory or on disk, and associate this data with a unique session
identifier (usually a cookie or URL parameter) sent to the client's
browser.
● Sessions are commonly used in web development to maintain user
login status, store shopping cart items, track user preferences, and
more.
Concept of Session

• Starting a session: session_start()


• Setting session variables: $_SESSION['key'] = 'value';
• Retrieving session variables: $value = $_SESSION['key'];
• Destroying a session: session_destroy()
Concept of Session
Usage of Session

Sessions are widely used in web development to manage user-specific


data and maintain user state across multiple pages or requests.

1. User Authentication and Login Sessions


2. Shopping Cart and E-commerce Sessions
3. User Preferences and Settings
4. User Tracking and Analytics
5. Form Data Persistence
6. Access Control and Permissions
7. Session Timeout and Security
Different ways to handle session
data
There are several ways to handle session data in web development,
each with its own advantages and considerations.

1. Using PHP Sessions:


● PHP provides built-in support for sessions through the
$_SESSION superglobal array.
Different ways to handle session
data

2. Using Cookies:
● Cookies can be used to store session identifiers or small
amounts of session data on the client-side.
Different ways to handle session
data

3. Using URL Parameters:


● Session IDs can be passed in URLs as query parameters to
maintain session state between requests.
● The server reads the session ID from the URL parameter to
identify the session and retrieve session data.
Different ways to handle session
data

4. Using Hidden Form Fields:


● Session IDs or small amounts of session data can be stored in
hidden form fields and submitted along with form data.
Different ways to handle session
data

5. Using AJAX Calls:


● AJAX calls can be used to send session data to the server
asynchronously without refreshing the entire page.
Session and Cookies

You might also like