PHP Part 4_V2
PHP Part 4_V2
Semester 3
IT 3505
CREATE TABLE Persons ( PID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName CHAR(15),
LastName CHAR(15), Age INT )
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Database connection failed: " . $conn-
• O
>connect_error);
}
echo "Success. Connected to database";
?>
IT3505 Web Application Development II
Close the Connection
• It’s always a best practice to close a
connection once you are done with working
with the database.
• Can close the connection using this syntax.
// if the connection object is
$conn
$conn->close();
mysqli_close($con);
?>
selects all data stored in the “persons" table and display only the
content of the ‘FirstName’ and ‘LastName’ columns.
<?php
$con=mysqli_connect("localhost","root","123456","bit");
if ($con->connect_error) die("Database connection failed:
" .
$conn->connect_error);
if(mysqli_query($con, "DELETE from Persons WHERE
FirstName='Nimal'")){
echo "Record delete successful";
} else {
echo "Error in executing the SQL" . $con->error;
}
mysqli_close($con);
?> IT3505 Web Application Development II
OOP using PHP
class Person{
public $name;
public $sex = "m"; //
default value
public $dob;
private $bank_account_no;
}
$c = new Person();
Classes should be defined before instantiation.
$c variable holds a reference to an instance (object) of the class
‘Person’.
$c->name = “Sunil”;
$p2->name = "Kamal";
echo $p1->name; // This will print the text “Kamal” as $p1 and $p2 points
to the same object
function __construct($name,$sex,$dob,$acc){
$this->name = $name; $this->sex = $sex;
$this->dob = new DateTime($dob);
//$dob should be give as "2015-01-15"
$this->bank_account_no = $acc; Example
}
public function print_age($toData){
//$toDate should be give as "2015-01-15"
$interval = $this->dob->diff(new DateTime($toDate));
echo "Years - ". $interval->y . " Months - ".$interval->m ." Days -
".$interval->d ;
}
}
function
__construct($name,$sex){ Example
$this->name = $name;
$this->sex = $sex;
}
Person::print_office();
?>
$c = new Circle();
print_r($c->center);
?> IT3505 Web Application Development II
The final Keyword
• There are cases where, you want to restrict a
subclass from redefining a member that exists
in a parent class. You can prevent properties
and methods from being redefined(overriding)
in a subclass by using the final keyword.
$c = new Circle();
echo $c->origin;
$s = new Shape();
echo $s->origin;
?>
IT3505 Web Application Development II
Interfaces
• Another new object-oriented feature in PHP5 is the
ability to create and use interfaces. Interfaces, in a
nutshell, are a way to specify what methods a class
must explicitly implement. This is useful when dealing
with many interconnected objects that rely on the
specific methods of one another.
• In PHP5, an interface is defined using the interface
keyword, and implemented using the implements
keyword.
• All methods declared in an interface must be public.
• Interfaces can be extended like classes using
the extends operator.
Your Code
Calls at
Calls when
appropriate
needed
places
Libraries Framework
Contains