PHP W-22
PHP W-22
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
Page 1 / 22 Page 2 / 22
www.diplomachakhazana.in
www.diplomachakhazana.in
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
Page 3 / 22 Page 4 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
Page 5 / 22 Page 6 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
Page 9 / 22 Page 10 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
$servername = "localhost";
$username = "root"; Step 5 : In the created Database, click on the 'Structure' tab. Towards
$password = ""; the end of the tables list, the user will see a 'Create Table' option.
$conn = new mysqli($servername, $username, $password); Give appropriate "Name" and "Number of Columns" for table and
if ($conn->connect_error) click on 'Go' button.
{
die("Connection failed: " . $conn->connect_error); Step 6 : Give details of columns based on their type. Enter the names
} for each column, select the type, and the maximum length allowed for
$sql = "CREATE DATABASE ifdept"; the input field. Click on "Save" in the bottom right corner. The table
if ($conn->query($sql) === TRUE) with the initialized columns will be created.
{
echo "Database created successfully"; 4. Attempt any THREE of the following: 12
} a) Define user defined function with example. 4M
else Ans. A function is a named block of code written in a program to perform
{ some specific tasks. They take information as parameters, execute a
Description
echo "Error creating database: " . $conn->error; block of statements or perform operations on these parameters and 2M, Example
} return the result. A function will be executed by a call to the function. 2M
$conn->close (); The function name can be any string that starts with a letter or
?> underscore followed by zero or more letters, underscores, and digits.
OR Syntax:
function function_name([parameters if any])
Steps using phpMyAdmin {
Step 1: Click on Start and select XAMPP from the list. Open Xampp Function body / statements to be executed
control panel by clicking on the option from the list. The Control }
Panel is now visible and can be used to initiate or halt the working of
any module. Example:
<?php
Step2: Click on the "Start" button corresponding function display() // declare and define a function
to Apache and MySQL modules. Once it starts working, the user can {
see the following screen: echo "Hello,Welcome to function";
Step 3: Now click on the "Admin" button corresponding to }
the MySQL module. This automatically redirects the user to a web display(); // function call
browser to the following address - http://localhost/phpmyadmin ?>
Step 4: Screen with multiple tabs such as Database, SQL, User When a function is defined in a script, to execute thefunction,
Accounts, Export, Import, Settings, etc. Will appear. Click on programmer have to call it with its name and parameters if required.
the "Database" tab. Give an appropriate name for the Database in the
first textbox and click on create option.
Page 11 / 22 Page 12 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
b) Write a program for cloning of an object. 4M Step 2) If user required to add CSS in <head> section.
Ans. (Any other correct program shall be considered) <head>
Correct (any other
program 4M <style> relevant steps
<?php to design web
.error {color: #FF0000;}
class student page shall be
{ </style> considered)
function getdata($nm,$rn) </head>
{ Step 3) In <body> section design form with all mentioned
$this->name=$nm; components.
$this->rollno=$rn; Step 4) using <?php
} Write script for validation for all required input field.
function display()
Save the file with php extension to htdocs (C:/Program
{
echo "<br>name = ".$this->name; Files/XAMPP/htdocs)
echo "<Br>rollno = ".$this->rollno; Note: You can also create any folders inside ‘htdocs’ folder and
} save our codes over there.
} Step 5) Using XAMPP server, start the service ‘Apache’.
$s1 = new student(); Step 6)Now to run your code, open localhost/abc.php on any web
$s1->getdata("abc",1); browser then it gets executed.
$s1->display();
$s2 = clone $s1; d) Explain queries to update and delete data in the database. 4M
echo "<br> Cloned object data "; Ans. Update data : UPDATE query
$s2->display(); Update command is used to change / update new value for field in Explanation
row of table. It updates the value in row that satisfy the criteria given of Update
?> query 2M
in query.
c) Write steps to create webpage using GUI components. 4M
The UPDATE query syntax:
Ans. Following are the GUI components to design web page:
Button - has a textual label and is designed to invoke an action Correct UPDATE Table_name SET field_name=New_value WHERE
when pushed. steps-4M field_name=existing_value
Example :
Checkbox - has textual label that can be toggled on and off.
UPDATE student SET rollno=4 WHERE name='abc'
Option - is a component that provides a pop-up menu of choices.
In the above query, a value from rollno field from student table is
Label - is a component that displays a single line of read-only,
updated with new value as 4 if its name field contains name as ‘abc’.
non-selectable text.
Scrollbar - is a slider to denote a position or a value.
Delete data: DELETE query
TextField - is a component that implements a single line of text. Explanation
Delete command is used to delete rows that are no longer required of
TextArea - is a component that implements multiple lines of text.
from the database tables. It deletes the whole row from the table. Delete query
To design web pages in PHP: The DELETE query syntax: 2M
Step 1) start with <html> DELETE FROM table_name WHERE some_column =
some_value
Page 13 / 22 Page 14 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
[WHERE condition] is optional. The WHERE clause specifies which Ans. PHP Code-
record or records that should be deleted. If the WHERE clause is not <?php For loop
syntax 2M
used, all records will be deleted. echo "Output<br>";
Example :- for($i=1;$i<=10;$i++) Correct
$sql = "DELETE FROM student WHERE rollno=2"; { syntax 2M
echo "$i<br/>";
In the above query, a row from student table is deleted if rollno field } Correct logic
2M
contains 2 in that row. ?>
e) Describe the syntax of if-else control statement with example in 4M Output
(Output is
PHP. 1
optional)
Ans. if-else control statement is used to check whether the 2
Description
condition/expression is true or false. Ifthe expression / condition of if-else
3
evaluates to true then true block code associated with the if statement control 4
is executed otherwise if it evaluates to false then false block of code statement 5
associated with else is executed. 2M, 6
Syntax: 7
Syntax1M,
if (expression/condition) 8
{ Example1M 9
True code block; 10
} b) Write a program to connect PHP with MYSQL. 6M
else Ans. Solution1:
{ <?php
False code block; $servername = "localhost"; Correct
} $username = "root"; syntax 2M
$password = "";
Example: // Connection Correct code
<?php $conn = new mysqli($servername,$username, $password); 4M
$a=30; // For checking if connection issuccessful or not
if ($a<20) if ($conn->connect_error)
echo "variable value a is less than 20"; {
else die("Connection failed: ". $conn->connect_error);
echo "variable value a is greater than 20"; }
Writing
?> echo "Connected successfully"; Output is
In the above example, variable a holds value as 30. Condition checks ?> optional
whether the value of a is less than 20. It evaluates to false so the Output:
output displays the text as ‘variable value a is greater than 20’. Connected successfully
5. Attempt any TWO of the following: 12 OR
a) Write a PHP program to display numbers from 1-10 in a 6M
sequence using for loop.
Page 15 / 22 Page 16 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
Solution2:
Create login.php Example:
<?php (Any type of inheritance example shall be considered)
$hostname = 'localhost'; <?php
$username = 'root'; class student {
$password = ''; var $var = "This is first var";
?> protected $fist_name;
protected $last_name;
Create db2.php file
<?php // simple class method
require_once 'login.php'; function returnVar() {
$conn = new mysqli($hostname, $username, $password); echo $this->fist_name;
//if ($conn->connect_error) die($conn->connect_error); }
if ($conn->connect_error) { function set_fist_name($fname,$lname){
die("Connection failed: " $this->fist_name = $fname;
. $conn->connect_error); $this->last_name = $lname;
} }
echo "Connected successfully"; }
?> class result extends student {
Output: public $percentage;
Connected successfully
function set_Percentage($p){
c) Illustrate class inheritance in PHP with example. 6M $this->percentage = $p;
Ans. Inheritance is a mechanism of extending an existing class where }
a newly created or derived class have all functionalities of function getVal(){
Definition /
existing class along with its own properties and methods. Explanation
echo "Name:$this->fist_name $this->last_name";
The parent class is also called a base class or super class. And the and echo "<br/>";
child class is also known as a derived class or a subclass. Types of echo "Result: $this->percentage %";
Inheritance allows a class to reuse the code from another class Inheritance- }
2M }
without duplicating it.
Reusing existing codes serves various advantages. It saves time, Any Correct
$res1 = new result();
cost, effort, and increases a program’s reliability. Program / $res1->set_fist_name("Rita","Patel");
To define a class inherits from another class, you use the extends example- 4M $res1->set_Percentage(95);
keyword. $res1->getVal();
Types of Inheritance: ?>
Single Inheritance Output:
Multilevel Inheritance Name:Rita Patel
Multiple Inheritance Result: 95 %
Hierarchical Inheritance
Page 17 / 22 Page 18 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
Page 19 / 22 Page 20 / 22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2005 Certified) (ISO/IEC - 27001 - 2005 Certified)
print_r($us_data);
Solution 2: ?>
<?php
// PHP program to count number of Output:a:3:{i:0;s:7:"Welcome";i:1;s:2:"to";i:2;s:3:"PHP";} Correct
// words in a string Array ( [0] => Welcome [1] => to [2] => PHP ) example of
$string = " This is a string "; insert query-
$str = trim($string); 3M
while (substr_count($str, " ") > 0) { ii) Query to insert data in the database
$str = str_replace(" ", " ", $str); <?php
} require_once 'login.php';
$len = substr_count($str, " ")+1; $conn = newmysqli($hostname,$username, $password,$dbname);
// Printing the result $query = "INSERT INTO studentinfo(rollno,name,percentage)
echo "Number of words in string: $len"; VALUES
?> ('CO103','Yogita Khandagale',98.45)";
$result = $conn->query($query);
Output: if (!$result)
Number of words in string: 4 die ("Database access failed: " . $conn->error);
c) i) State the use of serialization. 6M else
ii) State the query to insert data in the database. echo "record inserted successfully";
Ans. i) Use of serialization. ?>
Serialization
Serializing an object means converting it to a bytestream explanation
representation that can be stored in a file. Serialization in PHP is with
mostly automatic, it requires little extra work from you, beyond example- 3M Output:
calling the serialize () and unserialize( ) functions. record inserted successfully
Serialize() :
The serialize() converts a storable representation of a value.
The serialize() function accepts a single parameter which is the
data we want to serialize and returns a serialized string.
A serialize data means a sequence of bits so that it can be stored
in a file, a memory buffer or transmittedacross a network
connection link. It is useful for storing or passing PHP values
around without losing their type and structure.
Example:
<?php
$s_data= serialize(array('Welcome', 'to', 'PHP'));
print_r($s_data . "<br>");
$us_data=unserialize($s_data);
Page 21 / 22 Page 22 / 22