0% found this document useful (0 votes)
7 views

Php Lab Pgms

The document contains a series of PHP and MySQL lab programs designed for students at Rajadhani Degree College. Each program includes a brief description, code snippets, and expected outputs for various tasks such as printing 'hello world', checking for odd/even numbers, finding the maximum of three numbers, and more. The document serves as a practical guide for learning PHP scripting and database management.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Php Lab Pgms

The document contains a series of PHP and MySQL lab programs designed for students at Rajadhani Degree College. Each program includes a brief description, code snippets, and expected outputs for various tasks such as printing 'hello world', checking for odd/even numbers, finding the maximum of three numbers, and more. The document serves as a practical guide for learning PHP scripting and database management.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PHP/MYSQL LAB PROGRAMS

By Innahai Anugraham

RAJADHANI DEGREE COLLEGE


PHP & MySQL Lab Programs
1. Write a PHPscript to print “hello world”.
<!DOCTYPE>
<html>
<body>
<?php
echo "<h2>Hello World</h2>";
?>
</body>
</html>
Output:
Hello World

2 Write a PHPscript to find odd or even number from given number.


<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number= $_POST["number"];
if( $number % 2 == 0 )
{
echo"$number is an Even Number";
}
else
{
echo"$number is an Odd Number";
}
}
?>
<h1>Check Even or Odd Number</h1>
<form method="POST">
Enter Number: <input type="text" name="number"><br>
<input type="submit">
</form>
</body>
</html>
Output:

pg. 1
3 Write a PHPscript to find maximum of three numbers.
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n1= $_POST["number1"];
$n2= $_POST["number2"];
$n3= $_POST["number3"];
if ($n1 >= $n2 && $n1 >= $n3)
echo "$n1 is the maximum of $n1,$n2,$n3";
else if($n2 >= $n3)
echo "$n2 is the maximum of $n1,$n2,$n3";
else
echo "$n3 is the maximum of $n1,$n2,$n3";
}
?>
<h1>Maximum of Three Numbers</h1>
<form method="POST">
Enter Number1: <input type="text" name="number1" required><br/>
Enter Number2: <input type="text" name="number2" required><br/>
Enter Number3: <input type="text" name="number3" required><br/>
<input type="submit" value="Find Maximum">
</form>
</body>
</html>
Output:

4.Write a PHPscript to swap two numbers.


File:swap.php
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n1= $_POST["number1"];
$n2= $_POST["number2"];
echo "Before swapping:<br><br>";

pg. 2
echo "n1=".$n1." n2=".$n2;
$third = $n1;
$n1 = $n2;
$n2 = $third;
echo "<br><br>After swapping:<br><br>";
echo "n1=".$n1." n2=".$n2;
}
?>
<h1>Swapping Two Numbers</h1>
<form method="POST">
Enter Number1: <input type="text" name="number1" required><br/><br/>
Enter Number2: <input type="text" name="number2" required><br/>
<input type="submit" value="SWAP NUMBERS">
</form>
</body>
</html>
Ouput:

5. Write a PHPscript to find the factorial of a number


<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number= $_POST["number"];
$fact = 1;
echo "Factorial of $number:<br><br>";
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?>
<h1>Factorial of a Number</h1>
<form method="POST">

pg. 3
Enter Number: <input type="text" name="number" required><br/>
<input type="submit" value="Find Factorial">
</form>
</body>
</html>
Output:

6 .Write a PHPscript to check whether given number is palindrome or not.


<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num= $_POST["number"];
$rev=0;
$x=$num;
while( floor($num))
{
$rem=$num % 10;
$num=$num / 10;
$rev=$rev * 10 + $rem;
}
echo "The number = $x <br>";
echo "Reverse of the number = $rev <br>";
if ($x == $rev)
echo "The number is a palindrome <br>";
else
echo "The number is not a palindrome <br>";
}
?>
<h1>Check If the number is a Palindrome</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/><br>
<input type="submit" value="Check Palindrome">
</form>
</body>
</html>

pg. 4
Output:

7.Write a PHP script to reverse a given number and calculate its sum
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num= $_POST["number"];
$sum=0;
$rev=0;
$x=$num;
while( floor($num))
{
$rem=$num % 10;
$num=$num / 10;
$rev=$rev * 10 + $rem;
$sum=$sum + $rem;
}

echo "The number = $x <br>";


echo "Reverse of the number = $rev <br>";
echo "Sum of the digits is = $sum <br>";
}
?>
<h1>Reverse and sum of digits</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/><br>
<input type="submit" value="Find Sum">
</form>
</body>
</html>
Output:

pg. 5
8.Write a PHP script to to generate a Fibonacci series using Recursive function.
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$num= $_POST["number"];
echo "<h3>Fibonacci series using recursive function:</h3>";
echo "\n";
/* Recursive function for fibonacci series. */
function series($num){
if($num == 0){
return 0;
}else if( $num == 1){
return 1;
} else {
return (series($num-1) + series($num-2));
}
}
/* Call Function. */
for ($i = 0; $i < $num; $i++){
echo series($i);
echo "\n";
}
}
?>
<h1>Fibonacci Series</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/><br>
<input type="submit" value="Enter End of Series">
</form>
</body>
</html>
Output:

pg. 6
9.Write a PHP script to implement atleast seven string functions.

<?php
echo”<h1> String Operations </h1>”;
$text = "Hello, World!";
$length = strlen($text);
echo "The length of the string $text is $length characters. <br><br>";
$pos = strpos($text, "World");
echo "The word 'World' starts at position $pos. <br><br>";
$newText = str_replace("World", "Universe", $text);
echo "Replaced: $newText <br><br>";
$substring = substr($text, 0, 5);
echo "The first 5 characters are: $substring <br><br>";
$uppercase = strtoupper($text);
echo "Uppercase: $uppercase <br><br>";
$lowercase = strtolower($text);
echo "Lowercase: $lowercase <br><br>";
$reversed = strrev($text);
echo "Reversed: $reversed <br><br>";
?>
O/P:

10. Write a PHP program to insert new item in array on any position in PHP.
<!DOCTYPE>
<html>
<body>
<?php
//Original Array on which operations is to be perform
$original_array = array( '1', '2', '3', '4', '5' );
echo 'Original array :';
foreach ($original_array as $x)
{
echo "$x ";
}
echo "\n";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//value of new item

pg. 7
$inserted_value = $_POST["number"];
//value of position at which insertion is to be done
$position = $_POST["pos"];
//array_splice() function
array_splice( $original_array, $position, 0, $inserted_value );
echo "<br><br>After inserting $inserted_value in the array position $position is :";
foreach ($original_array as $x)
{
echo"$x ";
}
}
?>
<h1>Array Insertion</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/><br/>
Enter Position: <input type="text" name="pos" required><br/>
<input type="submit" value="Insert">
</form>
</body>
</html>
Output:

11. Write a PHPscript to implement constructor and destructor.


<?php
class Employee
{
/* Member variables */
var $name;
var $salary;

// Constructor
public function __construct($name,$salary){

pg. 8
echo("In constructor <br><br>");
$this->name=$name;
$this->salary=$salary;
}
// destructor
public function __destruct(){
echo "In Destructor<br>";
echo ("Name: $this->name and Salary: $this->salary");
}
}
// creating object
$employee_1 = new Employee("John","10000");
?>
Output

12. Write a PHP Script to implement form handling using GET method.
Getform.html
<!DOCTYPE html>
<html>
<h1>GET METHOD</h1>
<form method="GET" action="get.php">
Enter Name: <input type="text" name="name" required><br/><br/>
Enter Age: <input type="text" name="age" required><br/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
Get.php
<!DOCTYPE html>
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br/>
Your age is:<?php echo $_GET["age"]; ?>
</body>
</html>
Output

pg. 9
13. Write a PHP Script to implement form handling using GET method.
postform.html
<!DOCTYPE html>
<html>
<h1>POST METHOD</h1>
<form method="POST" action="post.php">
Enter Name: <input type="text" name="name" required><br/><br/>
Enter Age: <input type="text" name="age" required><br/><br/>
<input type="submit" value="submit">
</form>
</body>
</html>
post.php
<!DOCTYPE html>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br/>
Your age is:<?php echo $_POST["age"]; ?>
</body>
</html>
Output

pg. 10
14. Write a PHP script that receive form input by the method post to check the number
is prime or not.
<!DOCTYPE>
<html>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n= $_POST["number"];
$c=0;
for ($i = 1; $i <= $n; $i++)
{
if (($n % $i) == 0)
$c++;
}
if ($c == 2)
echo("$n is a Prime number<br/><br/>");
else
echo("$n is not a Prime number <br/><br/>");

}
?>
<h1>PRIME NUMBER CHECKING</h1>
<form method="POST">
Enter Number: <input type="text" name="number" required><br/>
<input type="submit" value="Find PRIME">
</form>
</body>
</html>
Output:

15. Write a PHP script that receive string as a form input


<!DOCTYPE>
<html>
<body>
<h1>RECEIVE STRING AS INPUT</h1>
<form method="POST">
Enter Name: <input type="text" name="name" required><br/>
<input type="submit" value="Print Name">
</form>

pg. 11
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n= $_POST["name"];

echo("Name Entered is $n");

}
?>
</body>
</html>
Output:

16. Write a PHP script to compute addition of two matrices as a form input.
<!DOCTYPE html>
<html>
<body>
<form method='get'>
<input type='submit' name='submit' value='MATRIX ADDITION'>
</form>
<?php

if(isset($_GET['submit']))
{
$a=array(
array(1,2,3),
array(1,2,3),
array(1,2,3)
);
$b=array(
array(1,2,3),
array(1,2,3),
array(1,2,3)
);
print "\n Matrix 1: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $a[$i][$j]," ";
}
echo "<br>";
}
echo "\n Matrix 2: <br>";

pg. 12
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $b[$i][$j]," ";
}
echo "<br>";
}
$sum=array();
echo "\n Sum of Matrices: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $a[$i][$j]+$b[$i][$j]," ";
}
echo "<br>";
}
}
?>
</body>
</html>
Output:

17 Write a PHP script to show the functionality of date and time function.
<!DOCTYPE>
<html>
<body>
<h1>Date function Implementation</h1>
<?php

pg. 13
echo "Time using date function: ".date("h:i:s");
echo "<br/>";
echo "Date and Time using date function: ".date("M,d,Y h:i:s A");
echo "<br/>";
echo "Time with am/pm:".date("h:i a");
echo "<br/>";
$timestamp = time();
echo("Timestamp is $timestamp");
echo "<br/>";
echo("Date with Timestamp:".date("F d, Y h:i:s A", $timestamp));
echo "<br/>";
echo "Timestamp using mktime:".mktime(23, 21, 50, 11, 25, 2017);
?>
</body>
</html>
Output:

Date function Implementation


Time using date function: 09:57:06
Date and Time using date function: Apr,29,2024 09:57:06 AM
Time with am/pm:09:57 am
Timestamp is 1714377426
Date with Timestamp:April 29, 2024 09:57:06 AM
Timestamp using mktime:1511648510
18. Write a PHP script to upload a File
Note: Create a folder “upload” in the current directory
<html>
<head>
<title>File Uploading</title>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$target_path = "upload/";
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path))
{
echo "File uploaded successfully!";
} else{
echo "Sorry, file not uploaded, please try again!";
} }
?>

pg. 14
<h1> FILE UPLOADING </h1>
<form method="post" enctype="multipart/form-data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image" name="submit"/>
</form>
</body>
</html>
Output:

19 Write a PHP script to implement database creation


<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br/><br/>";
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
Output:

pg. 15
20 Write a PHP script to create table
<?php
$host = 'localhost:3306';
$user = 'root';
$pass = '';
$dbname = 'test';

$conn = mysqli_connect($host, $user, $pass,$dbname);


if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/><br/>';

$sql = "create table emp(id INT AUTO_INCREMENT,name VARCHAR(20) NOT


NULL,emp_salary INT NOT NULL,primary key (id))";
if(mysqli_query($conn, $sql)){
echo "Table emp created successfully";
}else{
echo "Could not create table: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:

21. Develop a PHP program to design a college admission form using MYSQL database.
<html>
<head>
</head>
<body>
<h2>College Admission Form</h2>
<form method="post" >
<label>Name:</label><br>
<input type="text" name="name" required><br><br>

<label>Email:</label><br>
<input type="email" name="email" required><br><br>

<label>Phone:</label><br>
<input type="text" name="phone" required><br><br>

pg. 16
<label>Course:</label><br>
<select name="course">
<option value="Computer Science">Computer Science</option>
<option value="Engineering">Engineering</option>
<option value="Mathematics">Mathematics</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname='test';

$conn = mysqli_connect($host, $user, $pass,$dbname);

if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$course = $_POST['course'];

$sql = "INSERT INTO admissions VALUES ('$name', '$email', $phone, '$course')";

if ($conn->query($sql) === TRUE)


echo "New record created successfully";
else
echo "Error: " . $sql . "<br>" . $conn->error;
$conn->close();
}
?>

</body>
</html>

OUTPUT:
1. Create table “admissions” inside database “test” with columns-
>student_name,email,phone,course.

pg. 17
Record in Database:

pg. 18

You might also like