0% found this document useful (0 votes)
13 views3 pages

Manual

The document provides an overview of PHP arrays, including indexed, associative, and multidimensional types, along with examples. It explains sessions and cookies for user information storage, highlighting their differences and usage in authentication. Additionally, it covers PHP-MySQL CRUD operations and basic SQL commands, including how to connect PHP to a MySQL database.

Uploaded by

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

Manual

The document provides an overview of PHP arrays, including indexed, associative, and multidimensional types, along with examples. It explains sessions and cookies for user information storage, highlighting their differences and usage in authentication. Additionally, it covers PHP-MySQL CRUD operations and basic SQL commands, including how to connect PHP to a MySQL database.

Uploaded by

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

Detailed Assignment Answers

1. PHP Arrays
PHP arrays are data structures that allow you to store multiple values in a single variable.
There are three types of arrays in PHP: indexed, associative, and multidimensional arrays.

Indexed arrays use numeric keys, while associative arrays use named keys.

Example of an indexed array:

$fruits = array('Apple', 'Banana', 'Cherry');

echo $fruits[0]; // Outputs 'Apple'

Example of an associative array:

$person = array('name' => 'John', 'age' => 30);

echo $person['name']; // Outputs 'John'

PHP can be embedded in an HTML page using PHP tags (<?php ?>) inside an HTML
document.

Example:

<!DOCTYPE html>
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<h1>Welcome to My Page</h1>
<?php
$message = "Hello, World!";
echo "<p>$message</p>";
?>
</body>
</html>

2. Sessions and Cookies


Sessions and cookies are used to store user information:

1. Sessions store data on the server and are identified by a unique session ID.
Example of session for user authentication:
session_start();
$_SESSION['user'] = 'JohnDoe'; // Store user data
echo "Welcome, " . $_SESSION['user']; // Access user data

2. Cookies store data on the client’s browser.

Example of cookie for user authentication:


setcookie('username', 'JohnDoe', time() + (86400 * 30)); // Store cookie for 30 days
echo $_COOKIE['username']; // Access cookie data

GET vs POST
- GET sends data via URL parameters, making it less secure.
- POST sends data in the request body, suitable for sensitive information.

3. PHP-MySQL CRUD Operations


CRUD operations involve Create, Read, Update, and Delete operations.

Create Operation
$sql = "INSERT INTO users (name, email) VALUES ('John', '[email protected]')";
$conn->query($sql);

Read Operation
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

Update Operation
$sql = "UPDATE users SET email = '[email protected]' WHERE id = 1";
$conn->query($sql);

Delete Operation
$sql = "DELETE FROM users WHERE id = 1";
$conn->query($sql);

4. Basic SQL Commands and PHP-MySQL Connection


Basic SQL commands:
- CREATE DATABASE db_name;
- CREATE TABLE table_name (id INT, name VARCHAR(50));
- INSERT INTO table_name VALUES (1, 'John');
- SELECT * FROM table_name;
- UPDATE table_name SET name = 'Doe' WHERE id = 1;
- DELETE FROM table_name WHERE id = 1;

PHP Code to connect to MySQL:


<?php
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
echo 'Connection Successful';

You might also like