OST Lab Mannual
OST Lab Mannual
OST Lab Mannual
HOME PAGE
<html>
<head>
<title>Login page</title>
</head>
<body>
<br><br>
<h1 style="font-family:Comic Sans Ms;font-size:20pt;text-align:center;color:Blue;">Simple
Message Passsing Site</h1>
<br><hr><br>
<form name="login" method="post" action="msg1.php">
<table align="center" cellpadding="5" cellspacing="5">
<tr>
<td>Enter your name</td>
<td><input type="text" name="t1"/></td>
<tr>
<tr>
<td>Type your message</td>
<td><textarea name="t2"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Login"/></td>
<td align="center"><input type="reset" value="Cancel"/></td>
</tr>
</table>
</form>
</body>
</html>
MESSAGE PAGE
<?php
$a=$_POST["t1"];$b=$_POST["t2"];
?>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<br><br>
<div>
<label style="color:Green">Hello All!!!</label><?php echo "<h1
style='color:Red;'>$a</h1>";?>
<br>
<br>
<label style="color:Green">Your Message is :</label><?php echo "<h1
style='color:Red;'>$b</h1>";?>
</div>
</table>
</body>
</html>
OUTPUT
2. CONDITIONAL STATEMENTS – FINDING NUMBER SIGN
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Conditional Statements in PHP</title>
<style>
body
{
font-family: Arial, sans-serif;
margin: 20px;
}
h2
{
color: #2c3e50;
}
.result
{
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Check Number Sign</h1>
<form method="POST">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Check</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST['number'];
// Conditional statements to check if the number is positive, negative, or zero
if ($number > 0)
{
$result = "$number is a positive number.";
} elseif ($number < 0)
{
$result = "$number is a negative number.";
} else {
$result = "The number is zero.";
}
// Display the result
echo "<div class='result'>$result</div>";
}
?>
</body>
</html>
OUTPUT
SWITCH CASE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Day of the Week</title>
</head>
<body>
<h1>Find the Day of the Week</h1>
<form method="post">
<label for="dayNumber">Enter a number (1-7):</label>
<input type="number" name="dayNumber" min="1" max="7" required>
<br>
<input type="submit" value="Get Day">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$dayNumber = intval($_POST['dayNumber']);
// Determine the day of the week
switch ($dayNumber) {
case 1:
$day = "Monday";
break;
case 2:
$day = "Tuesday";
break;
case 3:
$day = "Wednesday";
break;
case 4:
$day = "Thursday";
break;
case 5:
$day = "Friday";
break;
case 6:
$day = "Saturday";
break;
case 7:
$day = "Sunday";
break;
default:
$day = "Invalid input. Please enter a number between 1 and 7.";
}
// Display the result
echo "<h2>Result:</h2>";
echo "The day is: $day";
}
?>
</body>
</html>
OUTPUT
FOR EACH LOOP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fruits List</title>
</head>
<body>
<h1>Enter Fruits</h1>
<form method="post">
<label for="fruits">Enter fruits (comma separated):</label>
<input type="text" name="fruits" required>
<br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Retrieve and sanitize user input
$fruitsInput = $_POST['fruits'];
// Convert the input string into an array
$fruits = explode(',', $fruitsInput);
// Trim whitespace and remove empty values
$fruits = array_map('trim', array_filter($fruits));
// Display the list of fruits using foreach loop
echo "<h2>List of Fruits:</h2>";
echo "<ul>";
foreach ($fruits as $fruit)
{
echo "<li>$fruit</li>";
}
echo "</ul>";
}
?>
</body>
</html>
OUTPUT
FOR LOOP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic For Loop Example</title>
</head>
<body>
<h1>Dynamic For Loop Input</h1>
<form method="post">
<label for="start">Start Value:</label>
<input type="number" name="start" required>
<br>
<label for="end">End Value:</label>
<input type="number" name="end" required> <br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$start = intval($_POST['start']);
$end = intval($_POST['end']);
echo "<h2>Dynamic For Loop Results:</h2>";
// For loop to iterate from start to end
for ($i = $start; $i <= $end; $i++) {
echo "Number: $i <br>";
}
}
?>
</body>
</html>
OUTPUT
DO WHILE LOOP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Do While Loop</title>
</head>
<body>
<h1>Display Message</h1>
<form method="post">
<label for="count">Enter a number:</label>
<input type="number" name="count" required>
<br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve user input
$count = intval($_POST['count']);
$i = 0;
do {
echo "WELCOME TO III BCA !<br>";
$i++;
} while ($i < $count);
}
?>
</body>
</html>
OUTPUT
3. DIFFERENT TYPES OF ARRAYS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Types of Arrays in PHP</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h2 {
color: #2c3e50;
}
pre {
background-color: #f9f9f9;
padding: 10px;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<h1>Different Types of Arrays in PHP</h1>
<?php
// 1. Indexed Array
$fruits = ["Apple", "Banana", "Cherry"];
echo "<h2>Indexed Array</h2>";
echo "<pre>";
print_r($fruits);
echo "</pre>";
// 2. Associative Array
$person = [
"name" => "John",
"age" => 30,
"city" => "New York"
];
echo "<h2>Associative Array</h2>";
echo "<pre>";
print_r($person);
echo "</pre>";
// 3. Multidimensional Array
$contacts = [
["name" => "John", "email" => "[email protected]"],
["name" => "Jane", "email" => "[email protected]"]
];
echo "<h2>Multidimensional Array</h2>";
echo "<pre>";
print_r($contacts);
echo "</pre>";
?>
</body>
</html>
OUTPUT
4. USER DEFINED FUNCTION – FIBONACCI SERIES
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fibonacci Series Generator</title>
</head>
<body>
<h1>Fibonacci Series Generator</h1>
<form method="POST">
<label for="number">Enter the number of terms:</label>
<input type="number" id="number" name="number" min="1" required>
<button type="submit">Generate</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$n = intval($_POST['number']);
$fibonacciSeries = generateFibonacci($n);
echo "<h2>Fibonacci Series (first $n terms):</h2>";
echo "<p>" . implode(", ", $fibonacciSeries) . "</p>";
}
function generateFibonacci($n)
{
$fibonacci = [];
// Starting values
$a = 0;
$b = 1;
for ($i = 0; $i < $n; $i++)
{
$fibonacci[] = $a;
// Calculate the next term
$next = $a + $b;
$a = $b;
$b = $next;
}
return $fibonacci;
}
?>
</body>
</html>
OUTPUT
4. USER DEFINED FUNCTION – PALINDROME CHECKER
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palindrome Checker</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
form {
margin-bottom: 20px;
}
input[type="text"] {
padding: 5px;
width: 200px;
}
button {
padding: 5px 10px;
}
p{
font-weight: bold;
}
</style>
</head>
<body>
<h1>Palindrome Checker</h1>
<form method="POST">
<label for="inputString">Enter a string:</label>
<input type="text" id="inputString" name="inputString" required>
<button type="submit">Check</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
function isPalindrome($string) {
// Remove non-alphanumeric characters and convert to lowercase
$cleanedString = preg_replace("/[^A-Za-z0-9]/", "", $string);
$cleanedString = strtolower($cleanedString);
READING A FILE
<html>
<head>
<title>reading a file using php</title>
</head>
<body>
<?php
$filename="temp.txt";
$file=fopen($filename,"r");
if($file==false){
echo("error in opening file");
exit();
}
$filesize=filesize($filename);
$filetext=fread($file,$filesize);
fclose($file);
echo("file size:$filesize bytes");
echo("<pre> $filetext</pre>");
?>
</body>
</html>
WRITING TO A FILE
<?php
$filename = "newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false ) {
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
?>
<html>
<head>
<title>Writing a file using PHP</title>
</head>
<body>
<?php
$filename = "newfile.txt";
$file = fopen( $filename, "r" );
if( $file == false ) {
echo ( "Error in opening file" );
exit();
}
$filesize = filesize( $filename );
$filetext = fread( $file, $filesize );
fclose( $file );
echo ( "File size : $filesize bytes" );
echo ( "$filetext" );
echo("file name: $filename");
?>
</body>
</html>
OUTPUT
CREATING A SESSION
VIEWING A SESSION
DESTROYING A SESSION
CREATION OF COOKIES
<html>
<head>
<title>PHP Cookies Example</title>
</head>
<body>
<FORM method="POST" action="firstexample.php">
Enter Name : <input type="text" name="name"><br/>
Enter Age : <input type="text" name="age"><br/>
Enter City : <input type="text" name="city"><br/>
<br/>
<input type="submit" name="Submit1" value="Create Cookie">
<input type="submit" name="Submit2" value="Retrive Cookie">
<input type="submit" name="Submit3" value="Delete Cookie">
</FORM>
<?php
if(isset($_POST["Submit1"]))
{
setcookie("name",$_POST["name"], time() + 3600, "/", "", 0);
setcookie("age", $_POST["age"], time() + 3600, "/", "", 0);
setcookie("city", $_POST["city"], time() + 3600, "/", "", 0);
echo "Cookies Created !!";
}
if(isset($_POST["Submit3"]))
{
setcookie("name","", time() - 3600, "/", "", 0);
setcookie("age", "", time() - 3600, "/", "", 0);
setcookie("city", "", time() - 3600, "/", "", 0);
}
if(isset($_POST['Submit2']))
{
if(isset($_COOKIE["name"]))
{
echo "Name = ".$_COOKIE["name"]."<br/>";
echo "Age = ".$_COOKIE["age"]."<br/>";
echo "City = ".$_COOKIE["city"]."<br/>";
}
else
{
echo "Cookies deleted !!";
}
}
?>
</body>
</html>
OUTPUT
CREATING A COOKIE
RETRIEVING A COOKIE
DELETING A COOKIE
1. TABLE WITH CONSTRAINTS
//Database name: samp
//Table name: users123
//Field name: id int(3) , imageidlongblob
//display-new.php
<!DOCTYPE html>
<html>
<head>
<title>Display Image</title>
<style>body {
background-color: lightpink;
}
table { width: 50%; margin: auto;
border-collapse: collapse;
}
th, td {
border: 1px solid black;padding: 8px;
text-align: center;
}
</style>
</head>
<body>
<center>
<h1>VOTE FOR ME</h1>
<form action="" method="post" enctype="multipart/form-data">
<table>
<thead>
<tr>
<th>Id</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<?php
// Database connection
$connection = mysqli_connect("localhost", "root", "", "samp");
// Check connectionif (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Query to fetch all records from users123
$query = "SELECT * FROM users123";
$query_run = mysqli_query($connection, $query);
// Fetch and display each record while ($row = mysqli_fetch_assoc($query_run)) {
$imageData = base64_encode($row['imageid']);
$mimeType = 'image/jpeg'; // Adjust this if your images are of a different type
?>
<tr>
<td><?php echo htmlspecialchars($row['id']); ?></td>
<td>
<imgsrc="data:<?php echo $mimeType; ?>;base64,<?php echo $imageData; ?>" alt="Image"
style="width:200px; height:100px;">
</td>
</tr>
<?php
}
// Close connectionmysqli_close($connection);
?>
</tbody>
</table>
</form>
</center>
</body>
</html>
OUTPUT
2. INSERTION, UPDATION AND DELETION
INSERTION
<?php
$servername = "localhost";
$username = "root"; // Replace with your database username
$password = ""; // Replace with your database password
$dbname = "employee_db";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
JOIN OPERATIONS
4. SUB QUERIES
<?php
// Database connection details
$conn = new mysqli("localhost", "root", "", "user234");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to fetch orders of customers whose name contains 'maha'
$sql_in = "SELECT orders.id AS order_id, orders.customer_id, orders.order_date, customers.name
FROM orders
JOIN customers ON orders.customer_id = customers.id
WHERE customers.id IN (SELECT id FROM customers WHERE name LIKE '%maha%')";
$result_in = $conn->query($sql_in);
echo "<h3>Orders of customers whose name contains 'maha':</h3>";
if ($result_in->num_rows > 0) {
// Output data of each row
while($row = $result_in->fetch_assoc()) {
echo "Order ID: " . $row["order_id"] . " - Customer Name: " . $row["name"] . " - Order Date: " .
$row["order_date"] . "<br>";
}
} else {
echo "No records found for customers with 'maha' in their name.<br>";
}
// Query to fetch orders of customers whose name does NOT contain 'maha'
$sql_not_in = "SELECT orders.id AS order_id, orders.customer_id, orders.order_date,
customers.name
FROM orders
JOIN customers ON orders.customer_id = customers.id
WHERE customers.id NOT IN (SELECT id FROM customers WHERE name LIKE '%maha%')";
$result_not_in = $conn->query($sql_not_in);
echo "<h3>Orders of customers whose name does NOT contain 'maha':</h3>";
if ($result_not_in->num_rows > 0) {
// Output data of each row
while($row = $result_not_in->fetch_assoc()) {
echo "Order ID: " . $row["order_id"] . " - Customer Name: " . $row["name"] . " - Order Date: " .
$row["order_date"] . "<br>";
}
} else {
echo "No records found for customers without 'maha' in their name.<br>";
}
// Close the connection
$conn->close();
?>
OUTPUT
CREATION OF CUSTOMERS TABLE
<?php
// Database connection
$servername = "localhost";
$username = "root"; // replace with your username
$password = ""; // replace with your password
$dbname = "database"; // replace with your database name
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Sample data insertion (optional, comment this if data already exists)
$insertData = "INSERT INTO products (name, price, created_at) VALUES
('Book', 250.60, NOW()),
('Pencil', 50.20, NOW()),
('Marker Pen', 75.01, NOW())";
$conn->query($insertData);
// STRING FUNCTIONS
echo "<h2>String Functions:</h2>";
$sql = "SELECT name,
UPPER(name) AS upper_name,
LOWER(name) AS lower_name,
LENGTH(name) AS name_length
FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"] . " - Upper: " . $row["upper_name"] .
" - Lower: " . $row["lower_name"] . " - Length: " . $row["name_length"] . "<br>";
}
} else {
echo "0 results";
}
// NUMERIC FUNCTIONS
echo "<h2>Numeric Functions:</h2>";
$sql = "SELECT price,
ROUND(price) AS rounded_price,
CEIL(price) AS ceiling_price,
FLOOR(price) AS floor_price
FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Price: $" . $row["price"] . " - Rounded: $" . $row["rounded_price"] .
" - Ceiling: $" . $row["ceiling_price"] . " - Floor: $" . $row["floor_price"] . "<br>";
}
} else {
echo "0 results";
}
// DATE FUNCTIONS
echo "<h2>Date Functions:</h2>";
$sql = "SELECT created_at,
DATE(created_at) AS creation_date,
TIME(created_at) AS creation_time,
YEAR(created_at) AS creation_year,
MONTH(created_at) AS creation_month
FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Created At: " . $row["created_at"] .
" - Date: " . $row["creation_date"] .
" - Time: " . $row["creation_time"] .
" - Year: " . $row["creation_year"] .
" - Month: " . $row["creation_month"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
OUTPUT