PHP
PHP
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
?>
</body>
</html>
Output :
2.) To calculate the sum of two numbers with and without using PHP function
With PHP
// Define the two numbers
$num1 = 10;
$num2 = 20;
Without PHP :
// Define a function to calculate the sum
function sum($num1, $num2) {
return $num1 + $num2;
}
Output :
3.) To find the greatest number among three number using PHP .
$num1 = 10;
$num2 = 20;
$num3 = 15;
} else {
Output :
// Call the function with the number whose factorial you want to find
$number = 5;
$result = factorial($number);
Output:
7.) Input user detail (id, name ,email , password) using form and display to other page using php .
<!DOCTYPE html>
<html>
<head>
<title>User Details Form</title>
</head>
<body>
<h2>User Details Form</h2>
<form action="display.php" method="post">
<label>ID:</label>
<input type="text" name="id"><br><br>
<label>Name:</label>
<input type="text" name="name"><br><br>
<label>Email:</label>
<input type="email" name="email"><br><br>
<label>Password:</label>
<input type="password" name="password"><br><br>
// Create a connection
$conn = new mysqli($host, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE db_user";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
// Close connection
$conn->close();
Output :
9.) Create table user(id, name, email, password) in the above database using PHP.
// Replace the values with your MySQL credentials
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'db_user';
// Create a connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create table
$sql = "CREATE TABLE user (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
)";
// Close connection
$conn->close();
Output :
10.)Insert five records in a table user with fields id, name, email, password using PHP.
// Replace the values with your MySQL credentials
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'db_user';
// Create a connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert records
$sql = "INSERT INTO user (name, email, password) VALUES
('John Doe', '[email protected]', 'password123'),
('Jane Smith', '[email protected]', 'password456'),
('Bob Johnson', '[email protected]', 'password789'),
('Alice Lee', '[email protected]', 'password012'),
('Tom Davis', '[email protected]', 'password345')
";
if ($conn->query($sql) === TRUE) {
echo "Records inserted successfully";
} else {
echo "Error inserting records: " . $conn->error;
}
// Close connection
$conn->close();
Output:
11.) Select or display user records by retrieving data from user table using PHP.
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'db_user';
// Create a connection
// Check connection
if ($conn->connect_error) {
// Retrieve data
$sql = "SELECT * FROM user";
$result = $conn->query($sql);
// Display data
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
} else {
// Close connection
$conn->close();
Output: