0% found this document useful (0 votes)
5 views5 pages

Week Three - Four

The document covers server-side scripting, particularly focusing on PHP and its integration with MySQL databases. It includes explanations of PHP syntax, control structures, functions, and how to connect to and interact with a MySQL database. Additionally, it provides practical exercises, quizzes, and further reading assignments to enhance student understanding of these concepts.

Uploaded by

mauahadaisa
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)
5 views5 pages

Week Three - Four

The document covers server-side scripting, particularly focusing on PHP and its integration with MySQL databases. It includes explanations of PHP syntax, control structures, functions, and how to connect to and interact with a MySQL database. Additionally, it provides practical exercises, quizzes, and further reading assignments to enhance student understanding of these concepts.

Uploaded by

mauahadaisa
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/ 5

Week Three: Server-Side Scripting

1. Introduction to Server-Side Scripting: Server-side scripting involves writing code that is executed on
the server rather than on the user's browser. This allows dynamic content generation and interaction
with databases. The most common languages for server-side scripting include PHP, Python, Ruby, and
Node.js.

Example: When a user submits a form on a website, the server processes the data (like login
credentials), checks against a database, and responds accordingly (e.g., logging the user in or showing an
error message).

2. Introduction to PHP: PHP (Hypertext Preprocessor) is one of the most popular server-side scripting
languages used to create dynamic websites. It is embedded into HTML and interacts with databases like
MySQL to manage content dynamically.

3. PHP Syntax and Variables:

 PHP Syntax: PHP code is embedded within HTML using <?php ... ?> tags. The PHP interpreter
processes the code inside these tags and returns the output.

 Variables: In PHP, variables store data that can be used and manipulated. A variable is declared
using the $ symbol followed by a name (e.g., $name). PHP is loosely typed, meaning variables do
not need a type declaration (e.g., string, int).

Example:

php

Copy code

<?php

$name = "Alice";

echo "Hello, " . $name;

?>

Output: "Hello, Alice"

4. Control Structures in PHP: PHP allows the use of typical control structures like:

 If Statements: Used for conditional execution.

 Loops: PHP supports for, while, and foreach loops for repeating a block of code.

Example:

php

Copy code

<?php
$num = 5;

if ($num > 3) {

echo "Number is greater than 3";

} else {

echo "Number is 3 or less";

?>

5. Functions in PHP: PHP allows developers to define reusable blocks of code using functions. A function
performs a specific task and can return a value.

Example:

php

Copy code

<?php

function greet($name) {

return "Hello, " . $name;

echo greet("Alice");

?>

Practicum / Lab Practical:

 Writing Simple PHP Scripts: Students will create a PHP script that accepts a name input from a
form, processes it on the server, and outputs a greeting message on the webpage.

End of Topic Quiz:

1. What is server-side scripting, and how does it differ from client-side scripting?

2. Write a simple PHP script that prints "Hello World" to the browser.

3. Explain how PHP variables are declared and used with an example.

4. What is the role of functions in PHP? Write a function that adds two numbers and returns the
sum.
Further Reading Assignments:

1. Chapter 3 of "Head First PHP & MySQL" by Robson & Freeman – This chapter covers PHP
fundamentals in detail, with practical examples and exercises.

2. PHP Manual: Read the official PHP documentation (https://www.php.net/docs.php) to gain a


deeper understanding of PHP syntax, variables, and functions.

3. Research Task: Explore different server-side scripting languages (e.g., Python, Ruby) and
compare them with PHP in terms of ease of use, performance, and popularity.

Week Four: Connecting to Databases

1. PHP-MySQL Integration: PHP can be used to interact with databases, particularly MySQL, which is
widely used for web applications. The connection between PHP and MySQL allows the retrieval,
manipulation, and storage of data in a database, making websites dynamic and data-driven.

2. Establishing a Database Connection: To interact with a MySQL database in PHP, we need to:

 Establish a Connection: PHP uses functions like mysqli_connect() or PDO (PHP Data Objects) to
create a connection to a MySQL database.

 Check for Errors: Always check if the connection was successful to avoid further errors in the
application.

Example:

php

Copy code

<?php

$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";

?>

3. Executing SQL Queries in PHP: Once a connection is established, SQL queries can be executed to
interact with the database. Common queries include SELECT, INSERT, UPDATE, and DELETE.

Example (Inserting data into a database):

php

Copy code

<?php

$sql = "INSERT INTO users (username, password) VALUES ('alice', 'password123')";

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

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

?>

4. Closing the Database Connection: It is important to close the database connection after completing
the required operations to free up resources.

Example:

php

Copy code

<?php

$conn->close();

?>

Practicum / Lab Practical:


 Connect PHP to MySQL Database: Students will write a PHP script to establish a connection with
a local MySQL database and display a success message upon a successful connection. They will
also insert a record into a table and fetch it back for display on the webpage.

End of Topic Quiz:

1. Explain the importance of connecting PHP with a database like MySQL.

2. Write a PHP script to connect to a MySQL database named "school_db" with the username
"admin" and password "admin123".

3. What SQL query would you use to insert a new user with the username "bob" and password
"password456" into a table named users?

4. How do you close a MySQL connection in PHP, and why is it important?

Further Reading Assignments:

1. Chapter 4 of "Head First PHP & MySQL" by Robson & Freeman – This chapter provides practical
examples of connecting PHP to a MySQL database and executing SQL queries.

2. PHP Manual on MySQLi Functions: Explore the MySQLi documentation to learn about more
advanced functions for interacting with MySQL databases in PHP.

3. Case Study: Research how e-commerce websites like Amazon use PHP and MySQL to manage
vast amounts of user and product data dynamically.

These notes for weeks three and four provide detailed insights into server-side scripting and database
integration using PHP and MySQL, with practical examples, quizzes, and further reading assignments to
support student learning.

You might also like