PHP/MySQL









HTTP is a stateless protocol. It is not possible to keep track between 2 requests.
Cookies are small files (chunk of information) which is created at client side by browser, as informed by server.
Cookies are used to store / remember user specific information. Mostly used for customization of web pages.


Each Cookie has a name and value (pair).
Two types of Cookies:


1) Permanent Cookie.
   Cookies which has some expiry time and which is available even if you turn off your P.C

   eg: setcookie( "uname",”admin”, time()+3600);
     
          uname: Cookie name
          admin: Cookie value
       time()+3600: expiry time




   
2) Temporary Cookie.
As soon as you close your browser, Cookies are deleted.
   
   eg: setcookie( "uname",”admin”);





<?php


   if ( !(isset( $_COOKIE['color'] ) ) )
    {
     if(isset( $_POST['txt1']))
      {
       setcookie( "color", $_POST["txt1"], time()+60);
       echo "<body bgcolor=". $_POST['txt1']. ">"; 
      }


     else
      {
     
       echo "<html><body><form name=frm method=post>Enter Color :<input type=text         


name='txt1'><input type=submit value='Submit'></form> </body></html>";


      }
   }
  else
   {
   echo "<body bgcolor=". $_COOKIE['color']. "> Welcome Back!"; 


   }


?>

 Write MySQL Commands: 


      1) Create database to store payroll information.
             
                 mysql> Create Database Payroll;


      2) Create table employee with suitable constraints.


                mysql> Create table emp(
                         empno numeric(4) primary key,
ename varchar(20) not null,
sal float(6,2),
joining_date date,
                        deptno numeric(4) references dept(dno)
               );




      3) Take backup of employee table in text file.


             mysql> select * INTO OUTFILE  'C:/data.txt' from user fields terminated by ‘,’ ;
     
     4) Update joining_date column with current date.


           mysql>  Update emp
                        Set joining_date=now();


     5) Create user mca and assign update rights to him for all tables in payroll database.


          mysql> create User mca@’localhost’ identified by ‘nopass’;


          mysql> grant update on payroll.*  to mca@’localhost’;


     6) List all employees whose salary is less than 10,000.


          mysql>Select * from emp where sal<10000


Write Short note (PHP):



1) Associative Array:
               An array is a collection of elements (key value pair) with same or different   data types.
              An array in which elements are  indexed by String.


Eg:
<?php


$color = array ( "sky" => "blue", "grass" => "green", "blood" => “red”);


// OR
$color[“sky”]=”blue”;
$color[“grass”]=”green”;
$color[“blood”]=” red”;




foreach($color as $key => $value ) 
 {
 echo "$key  has $value color <br>";
  }
?>


Output: 
sky has blue color 
grass has green color 
blood has red color




sort() method is used to sort an array elements.
array_merge() method is used to join two or more array.


Write a PHP script using associative aray to display Menu Card of a restaurant serving Snacks,Punjabi and Continental Dishes.




<html>
<body >
<h3 align='center'>Menu Card</h3>
<table width=300 align=center border=5  cellspacing=0 


style='font-weight:bold;font-family:verdana'>
<?php


$menus=array(
               "Snacks"=>array("Vadapav"=>10,"Pav bhaji"=>20,"Cut-Dosa"=>25),                                      
               "Punjabi"=>array("Paratha"=>35,"Lassi"=>20, Mix Veg"=>65),                                                   
          "Continental Dishes"=>array("Veg Biryani"=>100,"Rice Plate"=>40,"Paner                                            
                Masala"=>65,"Official Lunch"=>50) 
                  );                     
      
?>
<tr ><td>Menu Name</td><td>Price(Rs)</td></tr>


<?php


 $row=1;


foreach ( $menus as $menu ) 
   {


     foreach ( $menu as $name=>$price )
        {
          if($row%2==0)
              { print "<tr  ><td>$name:</td><td> $price </td>";
              }
          else
          {
              print "<tr bgcolor=#fffacd><td>$name:</td><td> $price</td>";
              }
      $row++;
        }   
 }


?>
</table>
</body>
<html>



Object Oriented PHP:


PHP supports many useful features of OOP.


Terminology:


A class is a template that can be used for creating objects.
An object is an instance of a class. class may have many instance.


The data i.e variables present in an object are called its properties. 
The functions 






i.e methods present in an object 

are called its methods.



class
A class is a collection of variables and functions operating on these variables. 
In PHP, class is defined using the following syntax


syntax:
class classname 
{
 $variable = value; 
     function functionname (arguments)
    {
      // statements
    }
 }


example:
class Movie
 {
      var $name;
    function get_name ( )
       {
             return $this->name;
        }
    function set_name ($some_name) 
       {
        $this->name = $some_name;
       }
}


We should  use the $this to retrieve the properties of the current object as well as to call other functions.




How to create Object in PHP:


$variable = new Classname(args);


example:
$mov = new Movie('Fastest 5');


Write a PHP script to Create Account class having Deposit and Withdraw method:


<?php
class Account
{
    static $accno;
function __Construct($name,$bal) //constructor
   {
    self::$accno+=1;
    $this->name=$name;
    $this->bal=$bal;
    }
    function deposit($amt)
      {
      $this->bal+=$amt;
      echo "<br>Balance Updated(+)<br>";
      }
 function withdraw($amt)
      {
      if ( $this->bal-$amt>500)
         {
           $this->bal-=$amt;
           echo "<br>Balance Updated(-)<br>";
         }
      else
           echo "<br>Insufficient Balance<br>";
      }
function getinfo()
 {
  echo "<br>Ac. No = ".  self::$accno." Name = ".$this->name." your Balance= ".  $this->bal;
 }
}
 $obj1=new Account("hemant",12000);
              $obj1->getinfo();
              $obj1->withdraw(1000);
              $obj1->getinfo();
$obj2=new Account("anagha",10000);
              $obj2->getinfo();
              $obj2->withdraw(1000);
              $obj2->getinfo();
 ?>



 Create Animal  class with eat (), grow () as 



methods. Create  Birds, Reptiles as subclass of Animal. 

Birds with fly () as method. 

 Reptiles with crawl ()  method




<?php
class Animal
{
  
function Animal($group)
   {
    $this->group=$group;
   }
    function getinfo()
      {
        echo "<br>Animal Group=".$this->group;
      }
      function eat($e)
      {
      $this->e=$e;
      }
      function grow($g)
      {
      $this->g=$g;
      }
 }
class Birds extends Animal
{
  
function Birds($group,$name,$g,$e)
   {
   $this->Animal($group);
    $this->name=$name;
    $this->grow=$g;
    $this->eat=$e;
   }
    function getinfo()
      {
        parent::getinfo();
        echo "<br>Bird Name=".$this->name;
           echo "<br>Bird eat=".$this->e;
              echo "<br>Bird grow=".$this->g;
      
      }
      function fly()
      {
      echo "<br>I am flying<br>";
          
        }    
  
  }

 class Reptiles extends Animal
{
  
function Reptiles($group,$name,$g,$e)
   {
   $this->Animal($group);
    $this->name=$name;
    $this->grow=$g;
    $this->eat=$e;
   }
    function getinfo()
      {
        parent::getinfo();
        echo "<br>Reptile Name=".$this->name;
      }
    
    
    function crawl()
      {
      echo "<br>I am Crawling<br>";
          
        }    
    
  }
   $obj1=new Birds(" Wild ","eagle","grain","s");
              $obj1->getinfo();
               $obj1->fly();      
$obj2=new Reptiles("Wild","Snake","rat","f");
              $obj2->getinfo();
              $obj2->crawl();
          
 ?>



Have a Question ? Need Help in College Assignments, Need Code that is not available here? Just leave a comment & get your code instantly.

Tips to Enhance Your Blog