Web Technology Assignment 4 PDF

Summary

This document is a practice assignment on web technologies, specifically focusing on PHP programming concepts. It covers client-side/server-side scripting, PHP variables, control statements, cookies, regular expressions, and database interactions. The assignment includes practical script examples demonstrating how to interact with a MySQL database using PHP.

Full Transcript

## Section 1 ### Differences between Client-side and Server-side Scripting Languages * **Client-side scripting** is executed on the user's browser and affects what the user's browser rather than on the server. It's used for interactions and visuals. The browser processes these scripts, which makes...

## Section 1 ### Differences between Client-side and Server-side Scripting Languages * **Client-side scripting** is executed on the user's browser and affects what the user's browser rather than on the server. It's used for interactions and visuals. The browser processes these scripts, which makes it fast for users but limited by processes restrictions and client capabilities. * **Server-side scripting** code is executed on the server before sending the content to the user's browser. It provides more security, as users cannot see the source code, and handles complex operations that rely on server resources. ### Describe the role of variables in PHP Variables in PHP are used to store data which can then be manipulated and retrieved within a script. PHP variables start with a dollar symbol, and they allow you to store values such as numbers, strings, arrays, etc. PHP variables are dynamically typed, meaning they don't require explicit data type declaration. ### What is the purpose of Control Statements in PHP ? Control statements guide PHP code execution. They allow decisions, loops, and conditions, letting the script respond to different situations dynamically, enabling actions based on logical conditions within the code. ### Explain the use of cookies in state management in web Cookies are small data files stored on a client's browser, helping maintain state across sessions. They track user preferences and login status, ensuring personalized experiences even after users close and reopen their browsers. ### What is a Regular Expression in PHP ? Regular expressions in PHP are patterns used to match strings, validate inputs, and find specific text within data. Functions like `preg_match()` use regex to search, replace, and manipulate strings efficiently based on specific criteria. ## Section 2 **Write a script to insert data into a MYSQL database and display the output on a webpage.** ```php <?php $conn = mysqli_connect('localhost', 'user', 'password', 'database'); // INSERT query $sql = "INSERT INTO users (name, email) VALUES('Jana', '[email protected]')"; mysqli_query($conn, $sql); // SELECT query to fetch the data $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); // Display the data echo "<table> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr>"; while ($row = mysqli_fetch_assoc($result)) { echo "<tr> <td>" . $row['id'] . "</td> <td>" . $row['name'] . "</td> <td>" . $row['email'] . "</td> </tr>"; } echo "</table>"; mysqli_ close($conn); ?> ``` ## Section 3 ### Explain Session Management using Session Variables in PHP Session variables in PHP manage user state across pages. Unlike cookies, session data is stored on the server, creating a unique ID for each user. This allows seamless handling of data in registration, authentication schemes, and user profiles, making sure to maintain user-specific data throughout browsing sessions. ### What is Client-side Scripting Client-side scripting is a method where scripts run on the user's browser before sending the pre-rendered content to the user. It is commonly used for front-end features, like user interface interactivity, form validation, data display, and enhancing page performance. ### Explain PHP's String Handling Capabilities PHP offers extensive functions for manipulating strings, making it versatile for text processing. These functions include finding the length of a string, extracting a substring, replacing characters within a string, searching for a specific pattern, and much more. ### Discuss the Process of Form Handling Capabilities in PHP using POST and GET methods PHP handles forms through POST and GET methods. These methods capture input from HTML forms. POST sends data invisibly in the HTTP body, suitable for sensitive information, while GET appends data to the URL. Both are necessary to enable PHP to receive, validate, and process form data. ### Explain how PHP interacts with a MySQL database (cover the process of insertion, selection, and updating data in a MySQL table using PHP) PHP interacts with a MySQL database through functions like `mysqli_connect()`, `mysqli_query()`, and `mysqli_fetch_assoc()`. These functions allow PHP to perform various operations like: * **Insertion**: Inserting new data entries into a table using `INSERT INTO` statement. * **Selection**: Retrieving data from tables using `SELECT` statement. * **Updating**: Modifying existing data in a table using `UPDATE` statement. * **Deletion**: Removing data entries from a table using `DELETE` statement. This example shows how PHP handles insertion, updating, and deletion, enabling dynamic data management: ```php <?php $conn = mysqli_connect('localhost', 'user', 'password', 'database'); // INSERT $sql = "INSERT INTO users (name, email) VALUES('Jana', '[email protected]')"; mysqli_query($conn, $sql); // UPDATE $sql = "UPDATE users SET email = '@example.com' WHERE name = 'John'"; mysqli_query($conn, $sql); // DELETE $sql = "DELETE FROM users WHERE name = 'John'"; mysqli_query($conn, $sql); mysqli_close($conn); ?> ``` This script demonstrates how PHP interacts with MySQL to manage databases, supporting standard CRUD (Create, Read, Update, Delete) operations via functions like `mysqli_connect()`, `mysqli_query()`, and `mysqli_fetch_assoc()`. PHP establishes a connection, executes SQL queries, and modifies data for manipulation. The `INSERT INTO` statement needs records, `DELETE` removes them, and `UPDATE` changes existing records.

Use Quizgecko on...
Browser
Browser