0% found this document useful (0 votes)
11 views14 pages

Web Technologies

Uploaded by

maneetrathee98
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)
11 views14 pages

Web Technologies

Uploaded by

maneetrathee98
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/ 14

Web Technologies

Got it! Let’s cover all the PHP and MySQL topics from your syllabus one by one in a clear,
easy-to-remember, exam-ready format. I’ll present it in a way that helps you quickly revise
and also write confidently in your exam.

Let’s begin with the first major section:

🟩 PART 1: PHP and MySQL Basics


1. Basic Commands in PHP (with Examples)
Introduction to PHP:

PHP stands for “Hypertext Preprocessor.”

It’s a server-side scripting language used to create dynamic web pages.

PHP code is written inside: <?php ... ?>

Example:

php

<?php
echo "Hello, World!"; // Output: Hello, World!
?>

Common PHP Commands:

Command Purpose Example

echo Outputs data to browser echo "Hi!";

print Outputs data (similar to echo) print "Hello";

include Includes external PHP file include "header.php";

require Same as include, but causes error if file not found require "config.php";

isset() Checks if a variable is set isset($name)

empty() Checks if variable is empty empty($email)

$_GET/$_POST Retrieve form data $_GET['username']

1/14
2. Connecting to MySQL Server

Use mysqli or PDO in PHP to connect to MySQL.

Example using mysqli:

php

<?php
$conn = mysqli_connect("localhost", "root", "", "mydatabase");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully!";
?>

3. Creating a Database

php

$sql = "CREATE DATABASE mydatabase";


if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
}

4. Selecting a Database

You select a DB while connecting:

php

$conn = mysqli_connect("localhost", "root", "", "mydatabase");

5. Listing All Databases

php

$result = mysqli_query($conn, "SHOW DATABASES");


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

2/14
echo $row[0] . "<br>";
}

6. Listing All Table Names

php

$result = mysqli_query($conn, "SHOW TABLES");


while ($row = mysqli_fetch_row($result)) {
echo $row[0] . "<br>";
}

7. Creating a Table

php

$sql = "CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
)";
mysqli_query($conn, $sql);

8. Inserting Data into Table

php

$sql = "INSERT INTO users (name, email) VALUES ('Alice', '[email protected]')";


mysqli_query($conn, $sql);

9. Altering Tables
Add a new column:

php

ALTER TABLE users ADD age INT;

Change column datatype:

php

3/14
ALTER TABLE users MODIFY name VARCHAR(200);

10. Writing Queries


Simple SELECT query:

php

$sql = "SELECT * FROM users";


$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}

11. Deleting a Database

php

$sql = "DROP DATABASE mydatabase";


mysqli_query($conn, $sql);

12. Deleting Data from Table

php

$sql = "DELETE FROM users WHERE name='Alice'";


mysqli_query($conn, $sql);

13. Deleting a Table

php

$sql = "DROP TABLE users";


mysqli_query($conn, $sql);

14. phpMyAdmin

GUI tool for MySQL administration.

Access via http://localhost/phpmyadmin.

4/14
Can be used to:

Create/delete databases and tables

Insert/edit/delete data

Run SQL queries

Export/import data

15. Common Database Bugs

Connection error: Wrong username/password

SQL syntax errors: e.g., missing commas or quotes

Table not found: typo in table name

Data type mismatch: inserting string into INT column

No data returned: WHERE condition may be wrong

🟨 Next Up:
Managing State – the next major section includes:

Problem of State in Web Apps

Passing info using Query Strings and URL Path

Cookies

Sessions

Serialization

Shall I proceed with that now?

Great! Let’s now go through the second major section:

🟨 PART 2: Managing State in Web Applications


❓ What does "state" mean in web applications?
State refers to remembering information across multiple page loads or visits.

HTTP is stateless → once a page is loaded, the server forgets the user unless we use
state management techniques.

🔴 The Problem of State in Web Apps

5/14
Each HTTP request is independent.

If a user logs in and goes to another page, the server “forgets” the login info unless we
manually track it.

Solution: Use mechanisms to maintain state between pages (like sessions, cookies,
query strings, etc.).

Let’s now study each state management technique one by one:

1. Passing Information via Query Strings

Query strings are added to the end of a URL using ? and &.

Format: ?key=value&key2=value2

Example:
⟶ URL: http://example.com/profile.php?name=Alice&age=20

Access in PHP:

php

<?php
$name = $_GET['name']; // "Alice"
$age = $_GET['age']; // "20"
?>

Use Case:

Useful for small data like search terms, filters, or simple navigation.

Not secure for sensitive data.

2. Passing Information via URL Path (a.k.a. URL Rewriting)

Similar to query strings but cleaner URLs.

Often handled using .htaccess in Apache servers.

Example:
URL: http://example.com/user/Alice

With URL rewriting, PHP might receive the name via $_GET or a route.

Note: This technique is more advanced and not as common in basic PHP exams.

3. Cookies

6/14
Cookies store small pieces of data on the user's browser.

They are sent back to the server on every request.

Limited to about 4KB data.

Set a cookie in PHP:

php

setcookie("username", "Alice", time() + 3600); // 1 hour

Get a cookie:

php

echo $_COOKIE["username"];

Delete a cookie:

php

setcookie("username", "", time() - 3600);

Use Case:

To remember user preferences, login info (not secure), or theme selection.

4. Sessions

Sessions store user data on the server, unlike cookies which store it on the client.

PHP generates a unique session ID and sends it to the browser as a cookie.

Data is stored in the $_SESSION array.

Start a session:

php

session_start();
$_SESSION["username"] = "Alice";

Access session data:

php

7/14
session_start();
echo $_SESSION["username"];

Destroy session:

php

session_start();
session_unset(); // Unset session variables
session_destroy(); // Destroy session

Use Case:

For login systems, shopping carts, authenticated user actions.

Difference between Cookies and Sessions:

Feature Cookies Sessions

Stored In Client (browser) Server

Security Less secure More secure

Data Capacity ~4KB Much larger

Access $_COOKIE $_SESSION

Lifetime Set manually Until browser closes or timeout

5. Serialization

Converts data into a storable format (usually for sessions or databases).

Commonly used when storing complex data structures like arrays or objects.

Serialize an array:

php

$myArray = ["name" => "Alice", "age" => 25];


$serializedData = serialize($myArray);

Unserialize:

8/14
php

$restoredArray = unserialize($serializedData);

Use Case:

Useful for storing complex session data.

🎯 Exam Tip:
Use cookies when you want to store small data on the browser (like "remember me").

Use sessions when you need secure and temporary storage (like login).

🟩 Example Flow: Login System Using Sessions


1. User logs in: check credentials, then store username in $_SESSION.

2. User visits profile.php: check if $_SESSION["username"] is set.

3. User logs out: session_destroy().

🟨 Summary Chart of State Management Techniques:


Technique Stored In Accessed Via Use Case

Query String URL $_GET Search filters, page IDs

Cookie Browser $_COOKIE Remember preferences

Session Server $_SESSION Login, shopping cart

URL Path URL $_GET (rewritten) SEO-friendly URLs

Serialization String Format serialize(), unserialize() Store complex data

✅ You now have exam-ready notes on:


PHP & MySQL (Part 1)

Managing State (Part 2)

Would you like a quick one-page final revision sheet with the most important code and
points from both sections?

9/14
Awesome! Here's your final 1-page Quick Revision Sheet for PHP, MySQL & State
Management (for exam writing):

🟢 PHP & MySQL – Quick Syntax & Concepts


1. Connect to MySQL

php

$conn = mysqli_connect("localhost", "root", "", "mydb");

2. Create a Database

php

mysqli_query($conn, "CREATE DATABASE mydb");

3. Select Database (done when connecting)

4. Show Databases

php

$result = mysqli_query($conn, "SHOW DATABASES");

5. Show Tables

php

$result = mysqli_query($conn, "SHOW TABLES");

6. Create Table

php

$sql = "CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50)
)";

10/14
7. Insert Data

php

mysqli_query($conn, "INSERT INTO users (name, email) VALUES ('Alice', '[email protected]')");

8. Select Data

php

$result = mysqli_query($conn, "SELECT * FROM users");

9. Alter Table

php

ALTER TABLE users ADD age INT;

10. Delete Data

php

DELETE FROM users WHERE name = 'Alice';

11. Drop Table / Database

php

DROP TABLE users;


DROP DATABASE mydb;

12. phpMyAdmin

GUI tool for MySQL: http://localhost/phpmyadmin

Create/edit/delete DBs, run queries, export/import

Common Errors:

Wrong credentials

11/14
Syntax errors

Table or DB not found

Data type mismatch

🟡 Managing State in PHP


1. Query String (in URL)
URL: page.php?name=Alice
Access:

php

$name = $_GET['name'];

2. Cookies
Set:

php

setcookie("username", "Alice", time() + 3600);

Get:

php

echo $_COOKIE["username"];

Delete:

php

setcookie("username", "", time() - 3600);

3. Sessions
Start:

php

12/14
session_start();
$_SESSION["username"] = "Alice";

Access:

php

echo $_SESSION["username"];

Destroy:

php

session_unset();
session_destroy();

4. Serialization
Convert array to string:

php

$serial = serialize($arr);

Back to array:

php

$arr = unserialize($serial);

🟣 Difference Table (Cookies vs Sessions)


Feature Cookies Sessions

Stored in Client (browser) Server

Size ~4KB Large

Secure? Less More

Access $_COOKIE $_SESSION

13/14
Feature Cookies Sessions

Use Save prefs Login/auth

🟩 Most Important PHP Tags & Commands


Start PHP:

php

<?php ... ?>

Print:
echo "Hello";

Superglobals:
$_GET, $_POST, $_SESSION, $_COOKIE

Include file:
include("header.php");

Good luck on your exam! If you’d like I can quiz you with some short or long questions now
for practice. Would you like a practice round?

14/14

You might also like