Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. <?php
  2. class Database {
  3. private $host = 'localhost';
  4. private $user = "sec_user";
  5. private $pass = "eKcGZr59zAa2BEWU";
  6. private $dbname = 'costreport_2013';
  7. private $dbh;
  8. private $error;
  9.  
  10. public function __construct(){
  11. //DSN
  12. $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  13. //options
  14. $options = array(
  15. PDO::ATTR_PERSISTENT => true,
  16. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  17. PDO::ATTR_EMULATE_PREPARES, false
  18. );
  19. try {
  20. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  21. } catch (PDOException $e)
  22. {
  23. $this->error = $e->getMessage();
  24. }
  25. }
  26.  
  27. public function query($query){
  28. $this->stmt = $this->dbh->prepare($query);
  29. }
  30.  
  31. public function bind($param, $value, $type = null)
  32. {
  33. if(is_null($type)){
  34. switch (true) {
  35. case is_int($value):
  36. $type = PDO::PARAM_INT;
  37. break;
  38. case is_bool($value):
  39. $type = PDO::PARAM_BOOL;
  40. case is_null($value):
  41. $type = PDO_PARAM_NULL;
  42. break;
  43. default:
  44. $type = PDO:: PARAM_STR;
  45. }
  46. }
  47. }
  48.  
  49. public function execute(){
  50. return $this->stmt->execute();
  51. }
  52.  
  53. public function resultset(){
  54. $this->execute();
  55. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  56. }
  57.  
  58. public function single(){
  59. $this->execute();
  60. return $this->stmt->fetch(PDO::FETCH_ASSOC);
  61. }
  62.  
  63. public function rowCount(){
  64. return $this->stmt->rowCount();
  65. }
  66.  
  67. public function lastInsertID(){
  68. return $this->dbh->lastInsertID();
  69. }
  70.  
  71. public function beginTransaction(){
  72. return $this->dbh->beginTransaction();
  73. }
  74.  
  75. public function endTransaction(){
  76. return $this->dbh->commit();
  77. }
  78.  
  79. public function cancelTransaction(){
  80. return $this->dbh->rollBack();
  81. }
  82.  
  83. public function debugDumpParams(){
  84. return $this->stmt->debugDumpParams();
  85. }
  86. }
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement