Advertisement
kura2yamato

Model

Apr 28th, 2022
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.58 KB | None | 0 0
  1. <?php
  2. error_reporting(0);
  3. /***
  4. table:
  5. CREATE TABLE `pendaftar` (
  6.   `id` int(11) NOT NULL,
  7.   `nik` char(16) NOT NULL,
  8.   `nama` varchar(150) NOT NULL,
  9.   `alamat` text NOT NULL,
  10.   `surel` varchar(250) NOT NULL
  11. ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
  12. ALTER TABLE `pendaftar`
  13.   ADD PRIMARY KEY (`id`),
  14.   ADD UNIQUE KEY `nik` (`nik`),
  15.   ADD UNIQUE KEY `sureal` (`surel`),
  16.   ADD KEY `nama` (`nama`);
  17. ALTER TABLE `pendaftar`
  18.   MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
  19. ***/
  20. class Db
  21. {
  22.     private $dbsetup;
  23.     public $conn;
  24.     private $sql;
  25.     public function __construct($dbsetup)
  26.     {
  27.         $this->dbsetup= $dbsetup;
  28.         $this->conn=false;
  29.     }
  30.    
  31.     public function db()
  32.     {
  33.         $dbsetup = $this->dbsetup;
  34.         if($this->conn) return $this;
  35.        
  36.         try {
  37.             // $db = new PDO("mysql:host=localhost;dbname=senggol", "root", "");
  38.             //die(print_r($dbsetup));
  39.             $this->conn = new PDO("mysql:host=" . $dbsetup['host'] . ";dbname=" . $dbsetup['name'] . "", $dbsetup['user'], $dbsetup['pass']);
  40.             $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  41.             //echo "Connection successful" . PHP_EOL . "";
  42.             // echo "Data Pendaftaran Pasar Senggol: " . PHP_EOL . "";
  43.             // $sql = "SELECT * FROM pendaftar";
  44.             // $pendaftar = $this->conn->query($sql);
  45.             // foreach ($pendaftar as $daftar) {
  46.             //     echo "Id: " . $daftar["idpendaftar"] . "" . PHP_EOL . "";
  47.             //     echo "Tahun: " . $daftar["tahun"] . "" . PHP_EOL . "";
  48.             //     echo "Bulan: " . $daftar["bulan"] . "" . PHP_EOL . "";
  49.             //     echo "NIK: " . $daftar["nik"] . "" . PHP_EOL . "";
  50.             //     echo "Nama: " . $daftar["nama"] . "" . PHP_EOL . "";
  51.             //     echo "Alamat: " . $daftar["alamat"] . "" . PHP_EOL . "";
  52.             //     echo "Surel: " . $daftar["surel"] . "";
  53.             // }
  54.             return $this;
  55.         } catch (PDOException $e) {
  56.             die("Connection failed : " . $e->getMessage());
  57.         }
  58.     }
  59.  
  60.     public function beginTransaction()
  61.     {
  62.         $this->conn->beginTransaction();
  63.         return $this;
  64.     }
  65.  
  66.     public function commit()
  67.     {
  68.         $this->conn->commit();
  69.         return $this;
  70.     }
  71.    
  72.     public function countRegisteration($data)
  73.     {
  74.         $nik = $data['nik'];
  75.         $sql = $this->conn->prepare("SELECT COUNT(*) FROM pendaftar WHERE nik=?");
  76.         $sql->bindValue(1, $nik);
  77.  
  78.         $this->sql = $sql;
  79.         return $this;
  80.     }
  81.  
  82.     public function st()
  83.     {
  84.         $this->sql->execute();
  85.         return $this;
  86.     }
  87.  
  88.     public function fetchColumn()
  89.     {
  90.         return $this->sql->fetchColumn();
  91.     }
  92.  
  93.     public function addRegistration($data)
  94.     {
  95.         $sql = "INSERT INTO pendaftar (nik, nama, alamat, surel) VALUES (?, ?, ?, ?)";
  96.         $sql = $this->conn->prepare($sql);
  97.         $sql->bindValue(1, @$data['nik']);
  98.         $sql->bindValue(2, @$data['nama']);
  99.         $sql->bindValue(3, @$data['alamat']);
  100.         $sql->bindValue(4, @$data['surel']);
  101.         $sql->execute( );
  102.          
  103.         return true;
  104.     }
  105.  
  106.     // public function lastInsertId()
  107.     // {
  108.         // return $this->sql->lastInsertId();
  109.     // }
  110.  
  111.     public function rollBack()
  112.     {
  113.         return $this->sql->rollBack();
  114.     }
  115.     public function lastInsertId()
  116.     {
  117.         return $this->conn->lastInsertId();
  118.     }
  119.  
  120.    
  121.     public function countRegistration($data)
  122.     {
  123.         $sql="select count(*) c from pendaftar where nik=?";
  124.         //var_dump($this->conn);
  125.         $this->sql = $this->conn->prepare($sql);
  126.         $this->sql->bindValue(1, $data['nik']??0);
  127.         return $this;
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement