Advertisement
Guest User

PDO

a guest
Dec 1st, 2012
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. <?php
  2. /* ____ _ _ __ _______ ____
  3. / ___|| |__ ___ | |\ \ / / ____| __ )
  4. \___ \| '_ \ / _ \| __\ \ /\ / /| _| | _ \
  5. ___) | | | | (_) | |_ \ V V / | |___| |_) |
  6. |____/|_| |_|\___/ \__| \_/\_/ |_____|____/
  7.  
  8. ShotWEB - Content Management System.
  9. Build on Monsma's MVC Application Framework.
  10.  
  11. Copyright (C) 2012 Monsma & Wesley. All rights reserved.
  12. Website: http://www.shothotel.nl/ - http://www.blackcoders.eu/
  13. */
  14.  
  15. if(!defined('IN_SHOTWEB') ? die('Sorry, you cannot access this file.') : null);
  16.  
  17. class DatabaseDriver implements iDatabaseDriver
  18. {
  19. public $Query, $STMT, $Connection, $Connected, $Param;
  20. private $Config, $hostname, $username, $password, $database;
  21.  
  22. public function __construct()
  23. {
  24. global $Application;
  25. $this->Config = $Application->Config['mysql'];
  26.  
  27. if($this->Connected == false)
  28. {
  29. $this->Connect();
  30. }
  31. }
  32.  
  33. public function Connect()
  34. {
  35. try
  36. {
  37. $dsn = "mysql:dbname={$this->Config['database']};host={$this->Config['hostname']}";
  38. $this->Connection = new PDO($dsn, $this->Config['username'], $this->Config['password'], array(PDO::ATTR_PERSISTENT => true));
  39. }
  40. catch(PDOException $Exception)
  41. {
  42. throw new exception($Exception->getMessage());
  43. }
  44.  
  45. $this->Connected = true;
  46. }
  47.  
  48. public function Disconnect()
  49. {
  50. if($this->Connected == true)
  51. {
  52. $this->Connection = null;
  53. $this->Connected = false;
  54. }
  55. }
  56.  
  57. public function Secure($value)
  58. {
  59. return stripslashes(htmlentities($value));
  60. }
  61.  
  62. public function Query($value)
  63. {
  64. $this->Query = $value;
  65.  
  66. if(!$this->STMT = $this->Connection->prepare($value))
  67. {
  68. throw new exception($this->STMT->error);
  69. }
  70.  
  71. return $this;
  72. }
  73.  
  74. public function Bind($param)
  75. {
  76. $this->param = $param;
  77.  
  78. if(!is_array($param))
  79. {
  80. throw new exception('Bind Error: bind the params in a array!');
  81. }
  82.  
  83. return $this;
  84. }
  85.  
  86. public function Run()
  87. {
  88. if(!$this->STMT->execute($this->param))
  89. {
  90. throw new exception($this->STMT->error);
  91. }
  92.  
  93. return new DatabaseObject(null, $this->STMT);
  94. }
  95. }
  96.  
  97. class DatabaseObject implements iDatabaseObject
  98. {
  99. private $STMT;
  100.  
  101. public function __construct($query, $link)
  102. {
  103. $this->STMT = $link;
  104. }
  105.  
  106. public function result()
  107. {
  108. return $this->STMT->fetchColumn();
  109. }
  110.  
  111. public function fetch_array()
  112. {
  113. return $this->STMT->fetch(PDO::FETCH_ASSOC);
  114. }
  115.  
  116. public function num_rows()
  117. {
  118. return $this->STMT->rowCount();
  119. }
  120. }
  121. ?>
  122.  
  123.  
  124. // Voorbeeld
  125.  
  126. $Query = "UPDATE user_stats SET DailyRespectPoints = ?, DailyPetRespectPoints = ?";
  127.  
  128. Core::$DB->Query($Query)->Bind(array($number, $number))->Run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement