Guest User

Untitled

a guest
Jul 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. <?php
  2. class MyDatabase
  3. {
  4. protected $hostname = 'localhost';
  5. protected $username = 'root';
  6. protected $password = 'root';
  7. protected $database = 'db_siswa';
  8.  
  9. private $koneksi;
  10.  
  11. function __construct(){
  12. try{
  13. $this->koneksi = new PDO("mysql:host=$this->hostname;dbname=$this->database",
  14. $this->username,
  15. $this->password);
  16. $this->koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. }catch(PDOException $e){
  18. echo 'Connection failed:'. $e->getMessage();
  19. }
  20. }
  21.  
  22. public function getData($query, $myParams = array()){
  23. $command = $this->koneksi->prepare($query);
  24. $command->execute($myParams);
  25. $command->setFetchMode(PDO::FETCH_ASSOC);
  26. $result = $command->fetchAll();
  27. return $result;
  28. }
  29.  
  30. public function execute($query, $myParams = array()){
  31. $command = $this->koneksi->prepare($query);
  32. $command->execute($myParams);
  33. }
  34.  
  35. function __destruct(){
  36. $this->koneksi = null;
  37. }
  38. }
Add Comment
Please, Sign In to add comment