PHP and MySQL Programs and MQP
PHP and MySQL Programs and MQP
Write a
PHP script to print hello world'.
Labl.php
<?php
1/Program to print Hello World
echo "Hello World";
?>
Output
Hello World
) Write a PHP script to find odd or even number from given number.
Lab2.php
<?php
I/ Program to check Odd or
Even number
Sn=5;
echo *Given number is
$n"."<br;
if ($n%2==1)
echo $n is Odd number";
else 0C=d
echo "$n is Even number";
?>
Output
Given number is 5
5 is Odd number
3.
Write a PHP script to find maximum of three numbers.
Lab3.php
<?php
/ Program to find maximum of three numbers
=
$a 10;
=
$b 20;
=
Sc 30;
echo "Given numbers are :""<br>";
echo a-$a""<br>";
echo "b=$b""<br>";
echo c=$c""<br>";
PHP &MYSOL
184
if ($a>$b)&& ($a>$c))
echo "$a is largest number","<br>":
else if (($b>$a) && ($b>$c))dodte
'<br>";
echo "$b is largest number",
else if (($c>$a) &&
($c>$b))
"$c is largest number", "<br>";
echo
else
$f-$f*$i; (0-ncolinl
Output
Given number is 5
Factorialof 5 is 120
Write a PHP script to check whether given
number is palindrome or not.
Lab6.php
<?php
1/Program to check given number is palindrome or not
Sn=353;
Sx=$n;
$s=0;
while ($n!=0)
{
Sr=Sn%10;
Ss=$s*10+Sr;
Sn=(int)($n/10); eodi noito
if($x==$s)
echo "$x is palindrome number"."<br>";
else
echo "$x is not a palindrome number"."<br>";
?>
Output
palindrome number
PHP &
MYSOI
186
a given number and calculate
its
to reverse sum.
7. Write a PHP script Lab7.php
and Sum
<?php
Reverse a given number calculate
to
/ Program
$n=972;
$x=$n;
number is $n"'<br>":
echo "Given
Srev=0;
$sum-0;
while ($n!=0)
Sr=$n%10;
Srev=$rev* 10+$r;
$sum=$sum+$r;
Sn=(int) (Sn/10);
09ta2to isio
echo "Reverse of $x is $rev""<br":
echo "Sum of $x is $sum";ch lao o ibeoe
?>
Output
echo $a."<br>";
$c =$a +$b;
fibo(Sn- 1, $b, Sc);
Sn =5;
echo "Given input value is:$n""<br>;
echo "Fibonacci Series are:"."<br>;
MANUAL
LAB 187
|fibo($n);
?>
Output
Given input value is:5
Fibonacci Series are:
2
3
Lab9.php
<?php
/Program for string functions
Sstr ="COMPUTERscience";
echo "1. Lengthof the string is: ". strlen ($str)."<br>";iotA
echo 2.Reverse String:".strrev($str)."'<br>'";
echo "3. Number of words in the string
are: ".str_word_count($str)."<br":
echo 4.Upper Case of the given string is:".
strtoupper($str)."<br>";
echo "5.Lower Case of the given string
is:". strtolower($str)."<br>";
echo "6. Converts first letter
of every word in the string to
uppercase:".
ucwords($str)."<br>";
echo "7.Position of 'Science'in string is: ".strpos($str, 'science'),"<br>";
nctiot
?>
Output
1. Length of the string is: 16
2. Reverse String: ecneics RETUPMOC
3. Number of words the string are: 2
in
4. Upper Case of the given string is:COMPUTER SCIENCE
5. Lower Case of the given string
is:computer science
0. Converts first letter of every word in the string to
uppercase:COMPUTER
Science
Lab10.php
<?php
/Program to insert new item in an array
Slist =array(10,20,30,40,50); oMod
PHP &MYSOL
188
'Original
array is:."<br>";
echo
foreach ($list as
$i)
echo "$i""
"
$item =35;
3, 0, $item);
array_splice($list,
$item the array is:""<brs".
echo <br>"."After inserting
foreach ($list as $i)
{
echo "$i"" ";
?>
Output
10 20 30 40 50
35 the array
After inserting is:
10 20 3035 40 50
and destructor.
11. Write a PHP scriptto implement constructor
Labll.php
<?php
I/ Constructorand Destructor
class laptop
{
var Scompany;
var $model;
var Sprice;
function construct ($x, $y, $z)
echo "Constructor"."<br>";
echo *=== ===""<br";o
$this->company=$x;
Sthis->model=$y;
$this->price=$z;
function display)iet
{
echo "Company name =Sthis->company""<br>";
echo Model number-$this->model""<br>";
echo "Price=$this->price""<br";
LAB MANUAL
189
function
destruct()
{
echo
echo <br>""Destructor""'<br>".
echo "Destroying
"<br;
echo "Destroying .$this->company."<br>";
$this->model."<br":
echo "Destroying .$this->price."<br";
Sob=new laptop(“Dell",
4455","35000");
$ob->display(); aolo
?>
Output
Constructor
Destructor
Destroying Dell
Destroying 4455
Destroying 35000
12.
Write a PHP script to
imnplement form handling using
get method.
Lab12.php
<html>
<body>
<h2>Using GET Method</h2>
<form method=GET
Name:<input type="text" name=Uname"> <br
Password: <input type='password"
name="Pword'> <br>
<inputtype='submit name="sub' value=Login'">
<form>
cCbbors T20ethS
<?php 209sborttom o
if(isset($_GET['sub'|)
|Sname=$_GET[Uname"]:
PHP&
MYSOI
190
Pword"l;
Spassword=$_GET[ && Spassword== Welcomne@123")
if ($name=="Department"
Successful"."<br>";
echo Login
Password'':
Username or
else
echo "Invalid
?>
</body>
</html>
Output
Login button:
Before selecting
c
localhost
X 3(8) What
O localhost/lab12.php
Login
O localhost/lab1 2.php?Uname..
Login
|Login Successful
method
form handling using post
13. Write a PHP script to implement
Labl3.php
<html>
<body>
<h2>Using POST MethodK/h2>
<form method=POST"
Name: <input type='text" name="Uname"> <br
191
LABMANUAL
type="password" name=Pword"'>
Password: <input <br>
name= 'sub"
<input type="submit value="Login">
</form>
<?php bod
:isset($ POST['sub )) soooof megoced
{
Sname=$_ POST["Uname"];:goomd
Spassword-$_ POST["Pword"];odu
if (Sname==Department" && Spassword=="Welcome @123")
echo Login Successful'""<brs":
else
echo "InvalidUsername or Password";
?>
<body>
<html>
Output
Before selecting Login button:
co lo x 9(12) V wsr PHPA X
O localhost/Mab13.php
localhost/lab13.php
Login Successful
PHP &
MYSQL
192
the
form input by method post to
4. Write a PHP scriptthat receives checkthe
is prime or
not
number Lab14.php
<html>
<body>
prime number or not</h3>
<h3>Program to check
<form method='POST"'>
type="text name="prime"> <br><br>
Name: <input
name='sub value="Submit>
<input type='submit"
</form>
<?php
if(isset($_POST['sub'|)
Sn=$_POST["prime"]:
Sc=0;
for($i=1;$i<=$n;$i++)
if(Sn%$i=-0)
Sc++;
if (Sc==2)
echo "$n is a Prime Number""<br>";
else
echo "$n is not a Prime Number" "<br>":
?>
</body>
</html>
Output:
Before selecting Submit button:
X
localhost/lab14.php
O localhost/lab14.php
Name:7
Submit
193
MANUAL
LAB button:
Submit 14.php
selecting CO localhost/lab
After
O localhost/lab14.php
not
prime number or
Program tocheck
Name:
Submit
<html>
<body>
string as a form input</h3>Ct
<h3>Program to receive
<form method="POST"'>
Name: <input type=*text"
name="input'> <br><br>O
<inputtype='submit" name='sub" value='Submit">
<form>
<?php
if(isset($_POST['sub']))
Sn=$_POST[input"];
echo Name of the Department is $n";
</body>
</html>
Output:
Before selecting Submit button:
X
co localhost/lab15,php
O localhost/lab15.php
Name:Computer Science
Submit
194 PHP &
MYSOL
O localhost/lab15.php
...
as a form input
Program to receive string
Name:
Submit
<?php
|/ Addition of two Matrices
if(isset($_POST[´Addition' |))s nnit eb siooTSOol
Srows=$_POST['rows']1:
Scols=$_POST['cols'];
SA=S_POST['A]:
$B=$_POST['B';
$C=0;
for($i=0;$i<$rows;$it+)
{
for(Sj=0;Sj<$cols:$j++)
SC[$i][Sj]=$A[$iJ[Sj]+$B[$i]ISj];
}
echo <h2>Matrix A B:</h2>"; +
echo <Ktable border=°1'";
for($i=0;$i<$rows;$i++)
echo *<t>":
for(Sj-0:$j<$cols;$j++)
{
echo<td>".$C[$iJ[$j]"</td>";
}
echo</r>";
}
echo <K/table>:
return;
MANUAI
LAB 195
<html>
<body>
ch3>Enter Rows and Columns</h3>
<form method="POST">
Rows:<input type='number
name='rows" required>
Columns:<input type="nuBer name="cols required'>
innuttype="'submit" name="dimension" value-"Submit'>
<form>
<body>
<html>
<php
if(isset($_POST['dimension'
|))
Srows=$_POST['rOws' };
Scols=$_POST['cols'];
echo "<h3>Enter Matrices</h3>":
echo <form method='POST'>;
echo <input type=hidden' name='rows' value=Srows>";
echo "<inputtypehidden' name='cols' value='Scols'>";
echo <h3>Matrix A</h3>";
for(Si-0;$i<$rows;$i++)
for(Sj-0:$j<$cols;$jt)
echo <br>":
for(Sj=0;$j<$cols:$j++)
echo
<br>";
196 PHP &
MYSOL
echo <br>";
echo <input type='submit' name='Addition' value='Addition'.
echo </form>";
}
?>
Output
localhost/lab16,php X
O localhost/lab16.php
EnterMatrices
Matrix A
3 3 3
3 3
3 3
MatrixB
2 2
2
Addition
3 localhost/lab16.php X
O localhost/lab16.php
MatrixAt B:ArOsL q
5|5|5
17. Write a PHP script to show the functionality of date and time function.
Lab17.php
<?php
echo Date and Time Functions""'<br>";
echo = "<br>";
Scurdate= date(d-m-Y"):;
197
LAB
MANUAI
Scurtime= time();
echo "Current Time:".(date("h:i:s",Scurtime),"<br>".
Sdatel=new DateTime(“15-04-2024');goheols
Sdate2=new DateTime("20-04-2024');i bebsolquiovoej
$x=$datel->dif(Sdate2);ikoteeoo bobsolgUalr
echo Date Difference :".Sx->format("%R%a days')."<br>";
bosg o belioe
Sdate=new DateTime(15-04-2024');
Sdate->modify('+5 days');
echo "DateModify:".$date->format('d-m-Y')"<br>"; bod
Sdate=new DateTime(“01-05-2024');
Sx=new Datelnterval('P5D');noiud oliH bsolaU atoaka
Sdate->sub($x);
echo "Date Subtraction:".$date->format('d-m-Y),"<br>";
echo : <br>":
?>
Output
Date and Time Functions
CurrentDate:10-05-2024
CurrentTime:09:28:33
Date Add Days:20/04/2024
Date Diference :+5 days
Date Modify:20-04-2024
.Writea
Date
Subtraction:26-04-2024etaot
Lab18.php
<html>
<body>
name=*file" id='Gle
Select a file to upload: <input type="file"
File name='sub'>
<input type="submit" value="Upload
</form>
<?php
if (isset($_POST['sub' ]))
$file-$_FILES['file']['name ]:
$flename=$_FILES['fle ]['tmp_name'];
Supload='c:l\xampp\l";
$file)
if(move_uploaded_file($flename, Supload.
echo "File Uploaded Successfully";
else
?>
</body>e
</html>
Output:
Before selecting Upload File button:
3 localhost/lab18.php
O localhost/lab18.php
O localhost/lab18.php
ybo
MANUAL
LAB
199
a PHP script to implement database
Write
19 create the database creation. (The
used to "College") following
program
Lab19.php
<?php
I/Program for
database creation
$host ="localhost":
Susername
Spassword =
="root;i ioDatovhsstds
ofd
Sconn new = mysqli($host,
Ssql="use college"; $username, $password);
Sresult-$conn->query ($sql);
if (Sresult
==0) iutaesoone bstse
$sql="create database
a-g college";
Sresult-Sconn->query ($sql):
ho if(Sresult) notsoileqsaldstorii
bhonecho "Database Created oitsoiloosolde
rob else Successfully'","<br>";
0h
echo "Could not
Create Database"
"<br>";
else
Output
Database Created
20. Successfully
Write a
PHP script to create table. (The
Dle "Staff
using the following program used to create the
database college" in
MySQL.
php Lab20.php
Program for
Shost
table
creation
oqo
Susername
=localhost";
=
Spassword
root";
=.
Sdbname='college";
Sconn=
new
Ssql="use mysqli(Shost,$username, Spassword);
$dbname:
if
(Sresult
==0)
Sresult-$conn->>query($sql):
echo eogo2.ot.&ola nolq
else Database
Does not obA3EADEosiay ooiiqo
exits."<br>;ouls OLG
200 PHP &
MYSQL
Sresult=$conn->query($sql):
if($result)
?>
Output
Table Created Successfully
21. Develop a PHP program to design a college admission form using MYSOr
database.
Lab2la.html
<html>
<body>
<h2>College Application Form</h2>
<form action="lab20b.php" method=POST">
Name of the Student:<input type="text name="sname" required> <br><br
Father'sName: <input type-"text" namne="fname" required> <br><br>
ad Mothers's Name: <input type='text" name="'mname" required> <br><br>
Date of Birth:<input type="date" name-"dob" required> <br><br>
Gender:<select name="gender">
<option value="Male"> Male </option>
<option value-"Female"> Female </option>
</select> <br><br>
Email:<input type="email" name-"'email" required> <br><br
Mobile:<input type="text" name='mobile" required> <br><br>
Address:<textarea name="address" required> </extarea><br><br
PUC Name: <input type=" text" name='cname" required> <br><br
Collage
PUC Percentage: <input type="text" name="percen" required> <br><br>
Course Applied:<selectname="course">
<option value-"B.Sc">B.Sc</option>
<option value="BCA'>BCA</option>
<option value="B.Com">B.Comslontion0agp
</select> <br><br>
MANUAL 201
LAB
"submit" name="sub'" value="Submit'>
type=" <br><br>
|<input
</form>
<body>
</html>
Lab2lb.php
<html>
<?php
if(isset($_POST['sub'))
=localhost":
Shost
Susername =root':
=
|Spassword
Sdbname="college";
Ssname=S_POST[“'sname");
Sfname=$_POST[“fname"];
Smname-$_ POST["mname"];
Sdob=$_POST["dob'"];
Sgender=$_POST['gender"]:
Semail=$_POST[“email'];
Smobile=$_POST["mobile;
Saddress=$_POST["address"];
|Scname-$_POST[“cname"];
Spercen=$_POST[percen'"];
Scourse=$_POST["course"];
=
Sconn new mysqli($host,$username,$password,
$dbname);
Ssql="use Sdbname";
Sresult=$conn->query
($sql);
if
(Sresult ==0)
echo Database Does not exits"."<br>;
else
|if(Sresult)
Sresult-$conn->query
($sq);
echo
"Record Inserted
else Successfully""<br>";
echo
"Could not insert record:"."<br>";
?>
PHP &
MYSQL
202
Output:
Submit button:
Before selecting localhost/lab20a.html
O localhost/lab20a.html
Form
CollegeApplication
James
Name of theStudent:
Father's Name:John
Mothers's Name:Jancy
Birth:e1/e1/ 2004
O
Date
of
Gender. Male
Email: [email protected]
Mobile:9988776655
KR Pura
Address. Bengaluru
College
PUC Collage Name: Govt. Pre University
98. 56
PUC Percentage:
Submit oldoa
O localhost//lab20b.php
The question paper pattern for theory paper has three sections.
1Unit-I 08 2
08 2 2 1
2 Unit-II
3 Unit-II 10 2 1
1
4 Unit-IV 07 2 1
1
1
1
5 Unit-V 09 1
:
Max.Marks 60
Section -A
(6x 2 =12)
I. Answer any six questions. Each question carries 2 marks.
1. What is PHP?
Section-B
Section-C
III. Answer any three questions. Each question carries 8 marks. (3x8 =24)
16. Explain data types with an example.
17. Discuss in detail multidimensional array with an example.
18. Write a note on:
i) Functions and variable scope.
ii) Recursive functions
Section-A
Max.Marks 60:
I. Answer any 6
questions.Each
1. Write any question carries
two features of PHP. 2 markS.
2. What is
variable? Give an
(6x2= 24)
3.Write the
syntax for switch example.w S iD ebo
4. What statement.
is an array?
5. What Give an
a function?
is example.
6. Explain Give the syntax.
any two
7. What string
is an functions with an example.
8. What object? Give
istable? an example. iD
9. What is
phpMyAdmin?
IIL.
Answer any 4 questions. Section - B
10.Write a Each
note on
11.Write a
note on
data type. question carries 6
marks.
12.
Explain theuse
array
functions.
(4x6=24)
13. of
Explain any six Include() and
14.Write library Require)
thefeatures
15. of
functions with functions.eo
an example.
Explain MySQL.
connecting to
MySQL and
selectingthe
database.
M.Answer
16.Writeany 3 Section-C
questions. Each
a note on
17.Explain historyand question carries 8
18. any two featuresof marks.
Write anote
on:
looping PHP.Co
statements with
ExN (3x8=
i) 2A)
Functions and syntax and
ii) example.
Recursive variable
19. scope.
Explain functions
20. different
types of
Explain
MySQL inheritance
function with with an
an example.utot at s
example.
228 PHP & MYSQL
ModelQuestion Paper-11|
VI Semester BCA
question carries
Section - B
IL. Answer any 4 questions. Each question
carries 6 marks. (4x6=24)
10. What is an expression? Explain with
example.
11. Explain any two if statements with syntax
12. Write a note on array functions.
and example.O5os
13. Explain any three date and time
functions with an example.
14.What is data type in MySQL?
Explain the numeric data type.
15. Write a note on MySQL functions.
Section -C
IIIL. Answer any 3 questions. Each question carries 8
16. Explain any four types of
operators with
marks. & =
(3 x 8 24)
17. Explain different types of example.oauo olon&o
arrays.
18. What is a function?
Explain creating and invoking
user defined function with an
example.
19. Explain types of
constructorswith an example.
20. a)Write the
features of
MySQL.
b) Updating Records usingPHP and MySQL.sbct02ta
SYLLABUS
BCA
DSC17 : PHP & MysQL
Course Credits
Total
:04
ContactHours: 52
Summative Assessment Marks 60
Formative Assessment Marks :40
:
60 Exam Duration:2:30 Hours
Exam Marks:
Unit - I
lotroduction to PHP: Introduction to PHP, History and Features of PHP, Installation & Configuration of PHP,
Embedding PHP code in Your Web Pages, Understanding PHP, HTML and White Space, Writing Comments
in PHP.Sending Data to the Web Browser, Data types in PHP, Keywords in PHP,Using Variables, Constants
Unit - II
Programming with PHP: Conditional statements: if, if-else, switch, The ? Operator, Looping statements:
while Loop., do-while Loop,for Loop
Arrays in PHP: Introduction- What is Array?, Creating Arrays, Accessing Array elements, Typesof Arrays:
Indexed v/s Associative arrays, Multidimensional arrays, Creating Array. Accessing Array, Manipulating
Arrays, Displaying array, Using Array Functions, Including and Requiring Files- use of Include) and
Require), Implicit and Explicit Casting in PHP. [12hours]
Unit -II
Using Functions, Class-Objects, Forms in PHP: Functions inPHP, Function definition, Creating and
invoking user-defined functions, Formal parameters versusactual parameters, Function and variable scope,
Recursion, Library functions, Date and Time Functions
Strings in PHP: What is String?,Creating and Declaring String,String Functions [10hours]
Unit -IV
Class&Objects in PHP:What is Class & Object, Creating and accessing a Class &Object, Object properties,
methods, Overloading, inheritance, Constructor and Destructor
object
REFERENCES B0OKS
PHP& MySQL for Dynamic Web Sites-
Fourth Edition By Larry Ulman.
Learning PHP, MySQL and JavaScript By Robin Nixon-O
REILLY Publications
Programming PHP By Rasmus Lerdorf,
D.
Kevin Tatroe, Peter Macintyre
SAMS Teach Yourself PHP in 24 bours, Author: MattZandstra, Sams Publishing