PHP With Mysql Lab Program
PHP With Mysql Lab Program
<!DOCTYPE html>
<html>
<head>
<title>program to print hello world</title>
</head>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<title>program check given number is even or odd</title>
</head>
<body>
<?php
$number=3;
if($number%2==0)
{
echo "$number is Even Number";
}
else
{
echo "$number is Odd Number";
}
?>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<title>program check maximum of two numbers</title>
</head>
<body>
<?php
// Input the three numbers
// and store it in variable
$number1 = 3;
$number2 = 7;
$number3 = 11;
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>program swap two numbers</title>
</head>
<body>
<?php
$a = 45;
$b = 78;
echo "before swapping:<br>";
echo "a =".$a." b=".$b;
// Swapping Logic
$third = $a;
$a = $b;
$b = $third;
echo "<br><br>After swapping:<br>";
echo "a =".$a." b=".$b;
?>
</body>
</html>
Output:
<html>
<head>
<title>Factorial Program using loop in PHP</title>
</head>
<body>
<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<body>
<?php
$original = 1221;
$temp = $original;
$reverse = 0;
while ($temp>1)
{
$rem = $temp % 10;
$reverse = ($reverse * 10) + $rem;
$temp = $temp/10;
}
if ($reverse == $original)
{
echo "$original is a Palindrome number";
}
else
{
echo "$original is not a Palindrome number";
}
?>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<head>
<title>PHP Program To find the Reverse and sum of a given number</title>
</head>
<body>
<?php
$original = 123;
$temp = $original;
$reverse = 0;
$sum=0;
while($temp>1)
{
$rem = $temp%10;
$reverse =($reverse*10)+$rem;
$sum=$sum+$rem;
$temp = $temp/10;
}
echo "Reverse of number $original is: $reverse";
echo "<br>Sum of digits of given number $original is: $sum";
return 0;
?>
</body>
</html>
</body>
</html>
<html>
<head>
<title>PHP Program To find the Fibonacci series of a given number</title>
</head>
<body>
<?php
function fibonacci($n) {
// base case
if ($n <= 1) {
return $n;
} else {
// recursive case
return fibonacci($n - 1) + fibonacci($n - 2);
}
}
// Number of terms in the series
$num_terms = 10;
Output:
?>
</html>
Output:
echo "\n";
$inserted_value = '11';
$position = 2;
</body>
</html>
Output:
class ExampleClass {
private $name;
// Constructor
public function __construct($name) {
$this->name = $name;
echo "Constructor called. Hello, {$this->name}!<br>";
}
// Destructor
public function __destruct() {
echo "Destructor called. Goodbye, {$this->name}!<br>";
}
// Method to greet
public function greet() {
echo "Hello, {$this->name}!<br>";
}
}
// Create an instance of ExampleClass
$instance = new ExampleClass("John");
<!DOCTYPE html>
<html>
<head>
<title>GET Form Handling</title>
</head>
<body>
<?php
// Below code checks if Name have the value.
if(isset($_GET["name"])) {
echo "Hi ". $_GET['name']. "<br><br>";
exit();
}
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>POST Form Handling</title>
</head>
<body>
<?php
// Below code checks if Name have the value.
if(isset($_POST["name"])) {
echo "Hi ". $_POST['name']. "<br><br>";
exit();
}
?>
</body>
</html>
<head>
<title>PHP Program To Check a given number is Prime number or Not</title>
</head>
<body>
<form method="post">
<input type="text" name="num" placeholder="Enter positive interger"/> </td>
<input type="submit" name="submit" value="Submit"/> </td>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$flag = 0;
for($i = 2; $i <= $n/2; $i++)
{
if($n%$i == 0)
{
$flag = 1;
break;
}
}
if($flag == 0)
echo "$n is a prime number";
else
echo "$n is not a prime number";
return 0;
}
?>
</body>
<!DOCTYPE html>
<html>
<head>
<title>String Input Form</title>
</head>
<body>
<h2>Enter a String</h2>
<form action = "<?php $PHP_SELF ?>" method = "POST">
<input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
<?php
// Check if form is submitted
if (isset($_POST["submit"])) {
// Check if the input field is not empty
if (!empty($_POST['name'])) {
$name = $_POST ['name'];
echo "<h2>You entered:</h2>";
echo "<p>$name</p>";
}
else
{
// If the input field is empty, display an error message
echo "<h2>Please enter a string</h2>";
}
}
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Matrix Addition</title>
</head>
<body>
<h2>Matrix Addition</h2>
<form method="post">
Enter the dimensions of the matrices:<br><br>
Rows: <input type="number" name="rows" min="1" required><br><br>
Columns: <input type="number" name="columns" min="1" required><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$rows = $_POST["rows"];
$columns = $_POST["columns"];
// Form for Matrix 1
echo '<h3>Enter values for Matrix 1:</h3>';
echo '<form method="post">';
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $columns; $j++) {
echo 'Value at position (' . ($i + 0) . ', ' . ($j + 0) . '): <input type="number"
name="matrix1[' . $i . '][' . $j . ']" required><br>';
}
}
echo '<input type="hidden" name="rows" value="' . $rows . '">';
// Processing addition
if(isset($_POST["matrix1"])) {
$matrix1 = $_POST["matrix1"];
Output:
Firstly you have to create a folder name called “uplodes” inside htdocs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Upload Form</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<!--multipart/form-data ensures that form data is going to be encoded as MIME
data-->
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="file" id="fileSelect">
<input type="submit" name="submit" value="Upload">
<!-- name of the input fields are going to be used in our php script-->
</form>
<?php
// Check if the form was submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// Check if file was uploaded without errors
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == 0)
{
if (!array_key_exists($ext, $allowed_ext))
die("Error: Please select a valid file format.");
Output:
<!DOCTYPE html>
<html>
<body>
<?php
// MySQL server configuration
$servername = "localhost"; // Change this if your MySQL server is on a different
host
$username = "root"; // Your MySQL username
$password = ""; // Your MySQL password
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE mydatabase1"; // Replace 'mydatabase' with the
name you want for your database
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
// Close connection
$conn->close();
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
// MySQL server configuration
$servername = "localhost"; // Change this if your MySQL server is on a different
host
$username = "root"; // Your MySQL username
$password = ""; // Your MySQL password
// Create connection
$conn = new mysqli($servername, $username, $password,);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql_create_db = "CREATE DATABASE mydatabase"; // Replace 'mydatabase'
with the name you want for your database
// Close connection
$conn->close();
?>
</html>
</body>
Output:
<!DOCTYPE html>
<html>
<head>
<title>College Admission Form</title>
<style>
.container {
max-width: 600px;
margin: 50px auto;
background-color: powderblue;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
h2 {
text-align: center;
color: blue;
}
form {
margin-top: 20px;
}
label {
font-weight: bold;
color: brown;
}
</style>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="phone">Phone:</label><br>
<input type="text" id="phone" name="phone" required><br><br>
<label for="address">Address:</label><br>
<textarea id="address" name="address" required></textarea><br><br>
<label for="gender">Gender:</label><br>
<select id="gender" name="gender" required>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select><br><br>
<label for="course">Course:</label><br>
<input type="text" id="course" name="course" required><br><br>
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}