Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(0);
- /***
- table:
- CREATE TABLE `pendaftar` (
- `id` int(11) NOT NULL,
- `nik` char(16) NOT NULL,
- `nama` varchar(150) NOT NULL,
- `alamat` text NOT NULL,
- `surel` varchar(250) NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
- ALTER TABLE `pendaftar`
- ADD PRIMARY KEY (`id`),
- ADD UNIQUE KEY `nik` (`nik`),
- ADD UNIQUE KEY `sureal` (`surel`),
- ADD KEY `nama` (`nama`);
- ALTER TABLE `pendaftar`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
- ***/
- class Db
- {
- private $dbsetup;
- public $conn;
- private $sql;
- public function __construct($dbsetup)
- {
- $this->dbsetup= $dbsetup;
- $this->conn=false;
- }
- public function db()
- {
- $dbsetup = $this->dbsetup;
- if($this->conn) return $this;
- try {
- // $db = new PDO("mysql:host=localhost;dbname=senggol", "root", "");
- //die(print_r($dbsetup));
- $this->conn = new PDO("mysql:host=" . $dbsetup['host'] . ";dbname=" . $dbsetup['name'] . "", $dbsetup['user'], $dbsetup['pass']);
- $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- //echo "Connection successful" . PHP_EOL . "";
- // echo "Data Pendaftaran Pasar Senggol: " . PHP_EOL . "";
- // $sql = "SELECT * FROM pendaftar";
- // $pendaftar = $this->conn->query($sql);
- // foreach ($pendaftar as $daftar) {
- // echo "Id: " . $daftar["idpendaftar"] . "" . PHP_EOL . "";
- // echo "Tahun: " . $daftar["tahun"] . "" . PHP_EOL . "";
- // echo "Bulan: " . $daftar["bulan"] . "" . PHP_EOL . "";
- // echo "NIK: " . $daftar["nik"] . "" . PHP_EOL . "";
- // echo "Nama: " . $daftar["nama"] . "" . PHP_EOL . "";
- // echo "Alamat: " . $daftar["alamat"] . "" . PHP_EOL . "";
- // echo "Surel: " . $daftar["surel"] . "";
- // }
- return $this;
- } catch (PDOException $e) {
- die("Connection failed : " . $e->getMessage());
- }
- }
- public function beginTransaction()
- {
- $this->conn->beginTransaction();
- return $this;
- }
- public function commit()
- {
- $this->conn->commit();
- return $this;
- }
- public function countRegisteration($data)
- {
- $nik = $data['nik'];
- $sql = $this->conn->prepare("SELECT COUNT(*) FROM pendaftar WHERE nik=?");
- $sql->bindValue(1, $nik);
- $this->sql = $sql;
- return $this;
- }
- public function st()
- {
- $this->sql->execute();
- return $this;
- }
- public function fetchColumn()
- {
- return $this->sql->fetchColumn();
- }
- public function addRegistration($data)
- {
- $sql = "INSERT INTO pendaftar (nik, nama, alamat, surel) VALUES (?, ?, ?, ?)";
- $sql = $this->conn->prepare($sql);
- $sql->bindValue(1, @$data['nik']);
- $sql->bindValue(2, @$data['nama']);
- $sql->bindValue(3, @$data['alamat']);
- $sql->bindValue(4, @$data['surel']);
- $sql->execute( );
- return true;
- }
- // public function lastInsertId()
- // {
- // return $this->sql->lastInsertId();
- // }
- public function rollBack()
- {
- return $this->sql->rollBack();
- }
- public function lastInsertId()
- {
- return $this->conn->lastInsertId();
- }
- public function countRegistration($data)
- {
- $sql="select count(*) c from pendaftar where nik=?";
- //var_dump($this->conn);
- $this->sql = $this->conn->prepare($sql);
- $this->sql->bindValue(1, $data['nik']??0);
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement