Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MyDatabase
- {
- protected $hostname = 'localhost';
- protected $username = 'root';
- protected $password = 'root';
- protected $database = 'db_siswa';
- private $koneksi;
- function __construct(){
- try{
- $this->koneksi = new PDO("mysql:host=$this->hostname;dbname=$this->database",
- $this->username,
- $this->password);
- $this->koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $e){
- echo 'Connection failed:'. $e->getMessage();
- }
- }
- public function getData($query, $myParams = array()){
- $command = $this->koneksi->prepare($query);
- $command->execute($myParams);
- $command->setFetchMode(PDO::FETCH_ASSOC);
- $result = $command->fetchAll();
- return $result;
- }
- public function execute($query, $myParams = array()){
- $command = $this->koneksi->prepare($query);
- $command->execute($myParams);
- }
- function __destruct(){
- $this->koneksi = null;
- }
- }
Add Comment
Please, Sign In to add comment