PHP Object Oriented Simple Example





Download Link

<?php
class YourTube {
    private $DbCon = null;
    public function __construct() {
        $this->DbCon = new PDO('mysql:host=localhost;dbname=google', 'root', '');
    }
    public function SqlSelect() {
        $Sql = "SELECT * FROM emp";
        $stmt = $this->DbCon->query($Sql);
        $stmt->execute();
        $data = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $data[] = $row;
        }
        return $data;
    }
}

<?php
require_once './youtube.php';
$Sql=new YourTube();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP My MVC Framework</title>
    </head>
    <body>
        <?php
        foreach($Sql->SqlSelect() as $post){
            print $post['id'].'<br/>';
        }
       ?>
    </body>
</html>
Out Put:-
1 Anil 18 30000.40
2 Rakesh 20 6000.40
3 Roshan 10 70000.40
4 Rajani 22 10000.40
5 Susant 19 5000.40
6 MUkesh 15 4000.40
7 Binita 50 3000.40
8 Binita 25 6500.40
Note:-
PDO::FETCH_NUM
PDO::FETCH_BOTH
PDO::FETCH_OBJ
--------------------------------
Eg:-
<?php

class YouTube {

    public $DbCon = null;
    public $id=1;
    public $name="Anil";
    public function __construct() {
        $this->DbCon = new PDO('mysql:host=localhost;dbname=google', 'root', '') or die(mysql_errno());
        if (!$this->DbCon) {
            print "Database Some Error";
        }
    }

    public function SqlSelect() {
        $sql = "SELECT * FROM emp WHERE id=? AND fname=?";
        $stmt=$this->DbCon->prepare($sql);
        $stmt->execute(array('1','Anil'));
        $data=array();
        while($row=$stmt->fetch()){
            $data[]=$row;
        }
        return $data;
    }

}

<?php
require_once './youtube.php';
$Sql = new YouTube();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP My MVC Framework</title>
    </head>
    <body>
        <?php
        foreach ($Sql->SqlSelect() as $post){
            //print $post['id'];
        }
        ?>
    </body>
</html>

Eg-2 Select With Parameter
<?php
class YourTube {
    private $DbCon = null;
    public function __construct() {
        $this->DbCon = new PDO('mysql:host=localhost;dbname=google', 'root', '');
    }
    public function SqlSelect() {
        $Sql = "SELECT * FROM emp WHERE id=:id AND fname=:fn";
        $stmt=$this->DbCon->prepare($Sql);
        $stmt->execute(array(':id'=>1,':fn'=>'Anil'));
        $data=array();
        while($row=$stmt->fetch()){
           $data[]=$row;
           // print_r($row);
        }
       return $data;
    }
}

<?php
require_once './youtube.php';
$Sql=new YourTube();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP My MVC Framework</title>
    </head>
    <body>
       <?php
       foreach ($Sql->SqlSelect() as $post){
           print $post['id'].' '.$post['fname'].' '.$post['age'].' '.$post['salary'].'<br/>';
       }
       ?>
    </body>
</html>
Out Put:-
1 Anil 18 30000.40
---------------------------------------
Eg: INSERT With Parameter

<?php
class YourTube {
    private $DbCon = null;
    private $id=9;
    private $firstname="Anmol";
    private $age=40;
    private $salary=4000.30;
    public function __construct() {
        $this->DbCon = new PDO('mysql:host=localhost;dbname=google', 'root', '');
    }
    public function SqlSelect() {
        $Sql = "INSERT INTO emp (id,fname,age,salary) VALUES (:Id,:fn,:age1,:salary1)";
        $stmt=$this->DbCon->prepare($Sql);
        $stmt->execute(array(':Id'=>$this->id,':fn'=>$this->firstname,':age1'=>$this->age,':salary1'=>$this->salary));
        if($stmt->rowCount()){
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
}
<?php
require_once './youtube.php';
$Sql=new YourTube();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP My MVC Framework</title>
    </head>
    <body>
      <?php
      if($Sql->SqlSelect()){
          print "Record Has Been Save....";
      }
      else{
          print "Sorry some Problem....";
      }
      ?>
    </body>
</html>
------------------------------
Eg: Update Record:-
---------------------------------
<?php
class YourTube {
    private $DbCon = null;
    private $id=3;
    private $firstname="Anmol";
    private $age=40;
    private $salary=4000.30;
    public function __construct() {
        $this->DbCon = new PDO('mysql:host=localhost;dbname=google', 'root', '');
    }
    public function SqlSelect() {
        $Sql = "UPDATE emp SET fname=:fn,age=:age1,salary=:salary1 WHERE id=:Id";
        $stmt=$this->DbCon->prepare($Sql);
        $stmt->execute(array(':fn'=>$this->firstname,':age1'=>$this->age,':salary1'=>$this->salary,':Id'=>$this->id));
        if($stmt->rowCount()){
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
}

<?php
require_once './youtube.php';
$Sql=new YourTube();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP My MVC Framework</title>
    </head>
    <body>
      <?php
      if($Sql->SqlSelect()){
          print "Record Has Been Save....";
      }
      else{
          print "Sorry some Problem....";
      }
      ?>
    </body>
</html>
-----------------------
Eg-Delete Query:
<?php
class YourTube {
    private $DbCon = null;
    private $id=3;
    private $firstname="Anmol";
    private $age=40;
    private $salary=4000.30;
    public function __construct() {
        $this->DbCon = new PDO('mysql:host=localhost;dbname=google', 'root', '');
    }
    public function SqlSelect() {
        $Sql = "DELETE FROM emp WHERE id=:Id AND fname=:fn";
        $stmt=$this->DbCon->prepare($Sql);
        $stmt->execute(array(':Id'=>$this->id,':fn'=>$this->firstname));
        if($stmt->rowCount()){
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
}

<?php
require_once './youtube.php';
$Sql=new YourTube();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP My MVC Framework</title>
    </head>
    <body>
      <?php
      if($Sql->SqlSelect()){
          print "Record Has Been Save....";
      }
      else{
          print "Sorry some Problem....";
      }
      ?>
    </body>
</html>
--------------------------
RowCount:-

class Demo {
    public $db = NULL;
    public function __construct() {
        $this->db=new PDO('mysql:host=localhost;dbname=mi', 'root', '');
    }
    public function select(){
        $data=array();
        $sql="select * from memberlogin ";
        $stmt=$this->db->query($sql);
        $stmt->execute();
        $num=$stmt->rowCount();
        print $num;
    }
}
Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment