Guest User

Untitled

a guest
Nov 15th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4. public $isConn;
  5. protected $datab;
  6.  
  7. //connect db
  8. public function __construct($username = "root", $password = "", $host = "localhost", $dbname = 'copart', $options = []){
  9. $this->isConn = TRUE;
  10. try {
  11. $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
  12. $this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  14. } catch (PDOException $e) {
  15. //throw new Exception($e->getMessage());
  16. echo $e->getMessage();
  17. }
  18. }
  19.  
  20. //disconnect
  21. public function Disconnect(){
  22. $this->datab = NULL;
  23. $this->isConn = FALSE;
  24. }
  25.  
  26. //get row
  27. public function getRow($query, $params = []){
  28. try {
  29. $stmt = $this->datab->prepare($query);
  30. $stmt->execute($params);
  31. return $stmt->fetch();
  32. } catch (PDOException $e) {
  33. echo $e->getMessage();
  34. }
  35. }
  36.  
  37. //get rows
  38. public function getRows($query, $params = []){
  39. try {
  40. $stmt = $this->datab->prepare($query);
  41. $stmt->execute($params);
  42. return $stmt->fetchAll();
  43. } catch (PDOException $e) {
  44. echo $e->getMessage();
  45. }
  46. }
  47.  
  48. //insert row
  49. public function insertRow($query, $params = []){
  50. try {
  51. $stmt = $this->datab->prepare($query);
  52. $stmt->execute($params);
  53. return TRUE;
  54. } catch (PDOException $e) {
  55. echo $e->getMessage();
  56. }
  57. }
  58.  
  59. //update row
  60. public function updateRow($query, $params = []){
  61. $this->insertRow($query,$params);
  62. }
  63.  
  64. //delete row
  65. public function deleteRow($query, $params = []){
  66. $this->insertRow($query,$params);
  67. }
  68. }
Add Comment
Please, Sign In to add comment