Inheritance Task

Create program to find area of Triangle using inheritance.

Solution:

<?php  
    class getdata  
    {  
        public $h;  
        public $b;  
        public function method1()  
        {  
            $this->h= $_REQUEST['height'];  
            $this->b= $_REQUEST['base'];  
        }  
  
    }  
    classfindarea extends getdata  
    {  
        public $area;  
        public function method2()  
        {  
            $this->area= ($this->h* $this->b)/2;  
            echo "Area of Triangle is : ".$this->area;  
        }  
    }  
    if(isset($_REQUEST['submit']))  
    {  
        $obj = new findarea();  
        $obj->method1();  
        $obj->method2();  
    }  
  
      
?>  
<html>  
<head>  
</head>  
<body>  
    <form method= "post">  
        <table align ="left" border="1">  
        <tr>  
            <td> Enter Height </td>  
            <td><input type = "text" name="height"/></td>  
        </tr>  
        <tr>  
            <td> Enter Base </td>  
            <td><input type = "text" name="base"/></td>  
        </tr>  
        <tr>  
            <td align ="center" colspan="2">  
            <input type= "submit"name="submit"value="click"/>  
        </td>  
        </tr>  
        </table>  
        </form>  
</body>  
</html>  

Output:

Inheritance Task
Inheritance Task
Inheritance Task

PHP MySQL Order By

PHP mysql_query() function is used to execute select query with order by clause. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

The order by clause is used to fetch data in ascending order or descending order on the basis of column.

Let’s see the query to select data from emp4 table in ascending order on the basis of name column.

  1. SELECT * FROM emp4 order by name  

Let’s see the query to select data from emp4 table in descending order on the basis of name column.

  1. SELECT * FROM emp4 order by name desc  

PHP MySQLi Order by Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'SELECT * FROM emp4 order by name';  
$retval=mysqli_query($conn, $sql);  
  
if(mysqli_num_rows($retval) > 0){  
 while($row = mysqli_fetch_assoc($retval)){  
    echo "EMP ID :{$row['id']}  <br> ".  
         "EMP NAME : {$row['name']} <br> ".  
         "EMP SALARY : {$row['salary']} <br> ".  
         "--------------------------------<br>";  
 } //end of while  
}else{  
echo "0 results";  
}  
mysqli_close($conn);  
?>  

Output:

Connected successfully
EMP ID :3 
EMP NAME : jai 
EMP SALARY : 90000 
--------------------------------
EMP ID :2 
EMP NAME : karan 
EMP SALARY : 40000 
--------------------------------
EMP ID :1 
EMP NAME : ratan 
EMP SALARY : 9000 
--------------------------------

PHP MySQL Select Query

PHP mysql_query() function is used to execute select query. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

There are two other MySQLi functions used in select query.

  • mysqli_num_rows(mysqli_result $result): returns number of rows.
  • mysqli_fetch_assoc(mysqli_result $result): returns row as an associative array. Each key of the array represents the column name of the table. It return NULL if there are no more rows.

PHP MySQLi Select Query Example

Example

 

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'SELECT * FROM emp4';  
$retval=mysqli_query($conn, $sql);  
  
if(mysqli_num_rows($retval) > 0){  
 while($row = mysqli_fetch_assoc($retval)){  
    echo "EMP ID :{$row['id']}  <br> ".  
         "EMP NAME : {$row['name']} <br> ".  
         "EMP SALARY : {$row['salary']} <br> ".  
         "--------------------------------<br>";  
 } //end of while  
}else{  
echo "0 results";  
}  
mysqli_close($conn);  
?> 

Output:

Connected successfully
EMP ID :1 
EMP NAME : ratan 
EMP SALARY : 9000 
--------------------------------
EMP ID :2 
EMP NAME : karan 
EMP SALARY : 40000 
--------------------------------
EMP ID :3 
EMP NAME : jai 
EMP SALARY : 90000 
--------------------------------

PHP MySQL Delete Record

PHP mysql_query() function is used to delete record in a table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Delete Record Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$id=2;  
$sql = "delete from emp4 where id=$id";  
if(mysqli_query($conn, $sql)){  
 echo "Record deleted successfully";  
}else{  
echo "Could not deleted record: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Record deleted successfully

PHP MySQL Update Record

PHP mysql_query() function is used to update record in a table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Update Record Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$id=2;  
$name="Rahul";  
$salary=80000;  
$sql = "update emp4 set name=\"$name\", salary=$salary where id=$id";  
if(mysqli_query($conn, $sql)){  
 echo "Record updated successfully";  
}else{  
echo "Could not update record: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Record updated successfully

PHP MySQL Insert Record

PHP mysql_query() function is used to insert record in a table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Insert Record Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'INSERT INTO emp4(name,salary) VALUES ("sonoo", 9000)';  
if(mysqli_query($conn, $sql)){  
 echo "Record inserted successfully";  
}else{  
echo "Could not insert record: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>

Output:

Connected successfully
Record inserted successfully

PHP MySQL Create Table

PHP mysql_query() function is used to create table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Create Table Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = "create table emp5(id INT AUTO_INCREMENT,name VARCHAR(20) NOT NULL,  
emp_salary INT NOT NULL,primary key (id))";  
if(mysqli_query($conn, $sql)){  
 echo "Table emp5 created successfully";  
}else{  
echo "Could not create table: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Table emp5 created successfully

PHP MySQL Create Database

Since PHP 4.3, mysql_create_db() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Create Database Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$conn = mysqli_connect($host, $user, $pass);  
if(! $conn )  
{  
  die('Could not connect: ' . mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'CREATE Database mydb';  
if(mysqli_query( $conn,$sql)){  
  echo "Database mydb created successfully.";  
}else{  
echo "Sorry, database creation failed ".mysqli_error($conn);  
}  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Database mydb created successfully.

PHP MySQL Connect

Since PHP 5.5, mysql_connect() extension is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_connect()
  • PDO::__construct()

PHP mysqli_connect()

PHP mysqli_connect() function is used to connect with MySQL database. It returns resource if connection is established or null.Syntax

  1. resource mysqli_connect (server, username, password)  

PHP mysqli_close()

PHP mysqli_close() function is used to disconnect with MySQL database. It returns true if connection is closed or false.

Syntax

  1. bool mysqli_close(resource $resource_link)  

PHP MySQL Connect Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$conn = mysqli_connect($host, $user, $pass);  
if(! $conn )  
{  
  die('Could not connect: ' . mysqli_error());  
}  
echo 'Connected successfully';  
mysqli_close($conn);  
?>  

Output:

Connected successfully