latihan php

Oleh 1201 0 0 pada Jumat, 20 Maret 2020, 06:40:03


Kembali ke forum soal ini

#php #selamat-datang-ke-php #berlatih-dengan-php


Petunjuk Diskusi
Silahkan masuk untuk berdiskusi



<?php 

  class Database
  {
    protected $connection;
    private $server = 'localhost';
    private $database = 'ukk_pengaduan_masyarakat';
    private $user = 'root';
    private $password = 'root';

    public function connect()
    {
      $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
      ];

      try {
        $this->connection = new PDO("mysql:host=$this->server;dbname=$this->database", $this->user, $this->password, $options);

        return $this->connection;
      } catch (PDOException $e) {
        echo $e->getMessage();
      }
    }

    public function disconnect()
    {
      $this->connection = null;
    }

    public function query($sql_query)
    {
      return $this->connect()->query($sql_query);
    }

    public function getAll($table)
    {
      return $this->connect()->query("SELECT * FROM $table")->fetchAll();
    }

  }
  
0 jempol


Belum ada tanggapan