Basic PHP Functions For Using Mysql: Result Function
Basic PHP Functions For Using Mysql: Result Function
Function Result
• $conn:
Index.html
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="fname" />
Lastname: <input type="text" name="lname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
An example for Data Insertion
Insert.php
<?php
$con=mysql_connect(“localhost","root","redhat");
mysql_select_db("parthi",$con);
if(!mysql_query($sql,$con))
mysql_close($con);
?>
An example for Data selection
<?php
$con=mysql_connect(“localhost","root","redhat");
if(!$con) {
mysql_select_db("parthi",$con);
mysql_close($con);
?>
An example for Using WHERE clause
<?php
$con = mysql_connect("localhost",“root",“redhat");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(“parthi", $con);
<?php
$con = mysql_connect("localhost",“root",“redhat");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(“parthi", $con);
$sql = mysql_query("SELECT * FROM student ORDER BY fname");
while($row = mysql_fetch_array($sql))
{
echo $row[‘fname'];
echo " " . $row[‘lname'];
echo " " . $row[‘age'];
echo "<br />";
}
mysql_close($con);
?>
An example for Updating data
<?php
$con = mysql_connect("localhost",“root",“redhat");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(“parthi", $con);
mysql_query("UPDATE student SET age = '36'
WHERE FirstName = ‘raja' AND LastName = ‘kumar'");
mysql_close($con);
?>
An example for Data deletion
<?php
$con = mysql_connect("localhost",“root",“redhat");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(“parthi", $con);
mysql_query("DELETE FROM Persons WHERE LastName=‘kumar'");
mysql_close($con);
?>
Creating a table using mysql
mysql>use test ;
Syntax:
mysql_close();
Or
mysql_close($conn);
Example:
• The variables that are associated with an object are called properties or
attributes
• A class constructor is a special function with the same name as its class that
is called automatically when an object from the class is instantiated
$Checking->getBalance();
$CheckNumber = 1022;
$Checking->getCheckAmount($CheckNumber);
Defining (declaring) a class
Use the “class” keyword which includes the class name (case-insensitive, but
otherwise following the rules for PHP identifiers). Note: The name “stdClass”
is reserved for use by the PHP interpreter.
• The functions and variables defined in a class are called class members
• Classes:
– Help make complex programs easier to manage
– Hide information that users of a class do not need to access or know about
– Make it easier to reuse code or distribute your code to others for use in
their programs
class Person
var $name;
function set_name($new_name)
function get_name() {
}
Declaring a class (cont.)
Use the “$this” variable when accessing properties and functions
of the current object. Inside a method this variable contains a
reference to the object on which the method was called.
Properties and functions can be declared as “public” (accessible
outside the object’s scope), “private” (accessible only by methods
within the same class), or “protected” (accessible only through the
class methods and the class methods of classes inheriting from the
class.
class Person
protected $name;
protected $age;
function set_name($new_name) {
function get_name() {
}
Declaring a class (cont.)
• Classes can also have their own constants defined (using the
“const” keyword), can have their own static properties and
functions (using the keyword “static” before “var” or “function”),
and can also can constructors and destructors (see below).
class HTMLtable {
HTMLtable::start();
?>
Accessing properties and methods
Once you have an object, you access methods and properties (variables) of
the object using the -> notation.
<?php
$age = 36;
?>
Using Access Specifiers
• Access specifiers control a client’s access to individual data members and
member functions
• There are three levels of access specifiers in PHP: public, private, and
protected
• The public access specifier allows anyone to call a class’s member function
or to modify a data member.
• The private access specifier prevents clients from calling member functions
or accessing data members and is one of the key elements in information
hiding
• Private access does not restrict a class’s internal access to its own members
$Checking->Balance);
$Cash = 200;
$Checking->withdrawal(200);
<?php
class Person {
protected $name;
protected $age;
function __construct() {
function __destruct() {
$DBConnect->close();
}
Inheritance
Use the “extends” keyword in the class definition to define a new object that inherits from another.
<?php
var $salary;
// of parent object
function update_salary($new_salary) {
?>
Inheritance (cont.)
• The constructor of the parent isn’t called unless the child explicitly references it (as in
this previous case). There is no automatic chain of calls to constructors in a sequence
of objects defined through inheritance.
• You could “hard-code” the call to the parent constructor using the function call
“Person::__construct($new_name, $new_age);” but it’s typically better to define it in
the manner given using the parent::method() notation. The same manner is used to call
the method of a parent that has been overridden by its child.
• You can use the “self” keyword to ensure that a method is called on the current class (if
a method might be subclassed), in this style self::method();
• To check if an object is of a particular class, you can use the instanceof operator.
// do something here
}
More on classes
• You can also define interfaces for objects (for which any object that uses that
interface must provide implementations of certain methods), and you can
define abstract classes or methods (that must be overridden by its children).
class Person {
var $name;
}
More on classes (cont.)
• There are methods for “introspection” about classes, i.e. the ability of a program to
examine an object’s characteristics.
For example,
$classes = get_declared_classes();
You can also get an array of method names in any of the following (equivalent)
manners:
$methods = get_class_methods(Person);
$methods = get_class_methods(‘Person’);
$class = ‘Person’;
$methods = get_class_methods($class);
More introspection functions
There are a wide variety of introspection functions, several more are
listed below.
• There are some “built-in” or predefined PHP method names that start in this
manner, most notably constructors and destructors using the __construct()
and __destructor() names. In the future (in new versions of PHP), it’s possible
that further methods might be defined that begin with a double underscore.
• Other reserved method names include __sleep() and __wakeup() which are
used for object serialization and __get() and __set(), which can be defined so
that if you try to access an object property that doesn’t exist, these methods
give an opportunity to either retrieve a value or set the (default?) value for that
property. For example, if a class is used to represent data obtained from a
database, you could write __get() and __set() methods that read and write data
whenever requested.