You are on page 1of 18

P ag e |1

ANSHUL GAUTAM

SRI GURU TEGH BAHADUR INSTITUTE


OF MANAGEMENT & INFORMATION
TECHNOLOGY
(Affiliated to GGSIPU)

BCA
WEB BASED
PROGRAMMING

ASSIGNMENT FILE
Submitted By: Submitted To:
NAME: ANSHUL GAUTUM PRABHJOT KAUR
CLASS: BCA 2C Asst. prof.

BCA-2C
P ag e |2
ANSHUL GAUTAM

PROGRAM-7
Ques: Do form handling in PHP design a form for personal
information then submit & retrieve the form using $_GET,
$_POST, $_REQUEST

SOURCE CODE:
<html>
<head></head>
<body>
<?php

<h1>REGISTRATION FORM</h1>
<form action="registration_form.php" method="POST">
First name:
<input type="text" name="firstname"> <br>
Last name:
<input type="text" name="lastname">
<input type="submit" value="Submit">

?>
</form>
</body>
</html>

OUTPUT:

BCA-2C
P ag e |3
ANSHUL GAUTAM

PROGRAM-8
Ques: WAP to perform all four sorting
[i]
SOURCE CODE:
<html>
<head></head>
<body>
<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
$arrlength = count($numbers);
for($x=0;$x<$arrlength;$x++){
echo $numbers[$x];
echo "<br>";
}
?>
</body>
</html>

OUTPUT:

BCA-2C
P ag e |4
ANSHUL GAUTAM

[ii]
SOURCE CODE:
<html>

<head></head>

<body>

<?php

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

krsort($age);

foreach($age as $x=> $x_value){

echo "Key=" .$x. ",Value=" . $x_value;

echo"<br>";

?>

</body>

</html>

OUTPUT:

BCA-2C
P ag e |5
ANSHUL GAUTAM

[iii]
SOURCE CODE:
<html>

<head></head>

<body>

<?php

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

asort($age);

foreach($age as $x=> $x_value){

echo "Key=" .$x. ",Value=" . $x_value;

echo"<br>";

?>

</body>

</html>

OUTPUT:

BCA-2C
P ag e |6
ANSHUL GAUTAM

[iv]
SOURCE CODE:
<html>

<head></head>

<body>

<?php

$numbers=array(4,6,2,22,11);

sort($numbers);

$arrlength = count($numbers);

for($x=0; $x<$arrlength; $x++){

echo $numbers[$x];

echo "<br>";

?>

</body>

</html>

OUTPUT:

BCA-2C
P ag e |7
ANSHUL GAUTAM

PROGRAM-9

Ques: WAP to create a file with fopen(), fclose(), fseek(),


fget(), ftell(), and fpassthru()

SOURCE CODE:
<html>
<head></head>
<body>

<?php

// Open the file in write mode


$file = fopen('example.txt', 'w');

// Check if the file was successfully opened


if ($file) {
// Write content to the file
fwrite($file, 'Hello, World!' . PHP_EOL);
fwrite($file, 'This is an example file.' . PHP_EOL);

// Close the file


fclose($file);

// Open the file in read mode


$file = fopen('example.txt', 'r');

// Check if the file was successfully opened


if ($file) {
// Read the content of the file using fgets()
while (($line = fgets($file)) !== false) {
echo $line;
}

// Reset the file pointer to the beginning of the file using fseek()
fseek($file, 0);

BCA-2C
P ag e |8
ANSHUL GAUTAM

// Get the current file pointer position using ftell()


echo 'Current position: ' . ftell($file) . PHP_EOL;

// Output the file contents using fpassthru()


fpassthru($file);

// Close the file


fclose($file);
} else {
echo 'Failed to open the file for reading.';
}
} else {
echo 'Failed to open the file for writing.';
}
?>
</body>
</html>

OUTPUT:

BCA-2C
P ag e |9
ANSHUL GAUTAM

PROGRAM-10
Ques: WAP to showcase the use of contructor
destructor in php oops?
CONTRUCTOR
SOURCE CODE:
<?php
class Fruit {
public $name;
public $color;

function construct($name, $color) {


$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}
}

$apple = new Fruit("Apple", "red");


echo $apple->get_name();
//br
echo " - ";
echo $apple->get_color();
?>

BCA-2C
P a g e | 10
ANSHUL GAUTAM

OUTPUT:

BCA-2C
P a g e | 11
ANSHUL GAUTAM

DESTRUCTOR
SOURCE CODE:
<?php
class Fruit {
public $name;
public $color;

function construct($name, $color) {


$this->name = $name;
$this->color = $color;
}
function destruct() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}

$apple = new Fruit("Apple", "red");


?>

OUTPUT:

BCA-2C
P a g e | 12
ANSHUL GAUTAM

PROGRAM-11
Ques: WAP to create a MySql database.

SOURCE CODE:
<?php
$servername = "localhost";
$username = "username";
$password = "password";

$conn = new mysqli($servername, $username, $password);


// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "CREATE DATABASE newDB";


if ($conn->query($sql) === TRUE) {
echo "Database created successfully with the name newDB";
} else {
echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

OUTPUT:

BCA-2C
P a g e | 13
ANSHUL GAUTAM

PROGRAM-12
Ques: WAP to create a table and insert few records into it
using form.
SOURCE CODE:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to create table


$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)";

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


echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

BCA-2C
P a g e | 14
ANSHUL GAUTAM

OUTPUT:

BCA-2C
P a g e | 15
ANSHUL GAUTAM

PROGRAM-13
Ques: Write a program to select all the records and display
the table.
SOURCE CODE:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " .
$row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

BCA-2C
P a g e | 16
ANSHUL GAUTAM

OUTPUT:

BCA-2C
P a g e | 17
ANSHUL GAUTAM

PROGRAM-14
Ques: WAP to modify (delete/modify/add) a table.
SOURCE CODE:
• Delete
<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// sql to delete a record


$sql = "DELETE FROM MyGuests WHERE id=3";

// use exec() because no results are returned


$conn->exec($sql);
echo "Record deleted successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

OUTPUT:

BCA-2C
P a g e | 18
ANSHUL GAUTAM

• Modify
SOURCE CODE:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

// Prepare statement
$stmt = $conn->prepare($sql);

// execute the query


$stmt->execute();

// echo a message to say the UPDATE succeeded


echo $stmt->rowCount() . " records UPDATED successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

OUTPUT:

BCA-2C

You might also like