Advertisement
Guest User

Untitled

a guest
Mar 5th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. <?php
  2. class MyPDO extends PDO
  3. {
  4.  
  5. public function __construct($dsn, $username = NULL, $password = NULL, $options = [])
  6. {
  7. $default_options = [
  8. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  9. PDO::ATTR_EMULATE_PREPARES => false,
  10. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  11. ];
  12. $options = array_merge($default_options, $options);
  13. parent::__construct($dsn, $username, $password, $options);
  14. }
  15. public function run($sql, $args = NULL)
  16. {
  17. if (!$args)
  18. {
  19. return $this->query($sql);
  20. }
  21. $stmt = $this->prepare($sql);
  22. $stmt->execute($args);
  23. return $stmt;
  24. }
  25. }
  26.  
  27.  
  28. class MyPDO extends PDO
  29. {
  30. public function run($sql, $args = NULL)
  31. {
  32. if (!$args) {
  33. return $this->query($sql);
  34. }
  35. $stmt = $this->prepare($sql);
  36. $stmt->execute($args);
  37. return $stmt;
  38. }
  39. }
  40.  
  41. //database connectie
  42. <!--//-->
  43. <!--//<!--//class Database {-->-->
  44. <!--//<!--// private $_connection;-->-->
  45. <!--//<!--// private static $_instance; //Enkel-->-->
  46. <!--//<!--// private $_host = "localhost";-->-->
  47. <!--//<!--// private $_username = "root";-->-->
  48. <!--//<!--// private $_password = "";-->-->
  49. <!--//<!--// private $_database = "url";-->-->
  50. <!--//<!--// /*-->-->
  51. <!--//<!--// Get an instance of the Database-->-->
  52. <!--//<!--// @return Instance-->-->
  53. <!--//<!--// */-->-->
  54. <!--//<!--// public static function getInstance() {-->-->
  55. <!--//<!--// if(!self::$_instance) { // Als er nog geen connectie bestaad-->-->
  56. <!--//<!--// self::$_instance = new self();-->-->
  57. <!--//<!--// }-->-->
  58. <!--//<!--// return self::$_instance;-->-->
  59. <!--//<!--// }-->-->
  60. <!--//<!--// // Constructor-->-->
  61. <!--//<!--// private function __construct() {-->-->
  62. <!--//<!--// $this->_connection = new mysqli($this->_host, $this->_username,-->-->
  63. <!--//<!--// $this->_password, $this->_database);-->-->
  64. <!--//<!--//-->-->
  65. <!--//<!--// // Error handling-->-->
  66. <!--//<!--// if(mysqli_connect_error()) {-->-->
  67. <!--//<!--// trigger_error("Fout bij het verbinden met MySQL: " . mysqli_connect_error(),-->-->
  68. <!--//<!--// E_USER_ERROR);-->-->
  69. <!--//<!--// }-->-->
  70. <!--//<!--// }-->-->
  71. <!--//<!--// // Magic method clone is empty to prevent duplication of connection-->-->
  72. <!--//<!--// private function __clone() { }-->-->
  73. <!--//<!--// // Get mysqli connection-->-->
  74. <!--//<!--// public function getConnection() {-->-->
  75. <!--//<!--// return $this->_connection;-->-->
  76. <!--//<!--// }-->-->
  77. <!--//<!--//}-->-->
  78. <!--//<!--//-->
  79. <?php
  80. if(stristr($_SERVER['REQUEST_URI'], 'connection.php')){
  81. die("Wait a minute... who are you? You're not allowed to come here.");
  82. }
  83.  
  84. //TODO omzetten naar pdo
  85. class Database {
  86.  
  87. // Connection information.
  88. private $connection;
  89.  
  90. // SQL querey information.
  91. private $querey;
  92.  
  93. // Connected to the database server.
  94. private $connected = false;
  95.  
  96. // Errors.
  97. private $error;
  98.  
  99. // Hostname or IP address of the database server.
  100. private $host = "localhost";
  101.  
  102. // Name of the database.
  103. private $database = "url";
  104.  
  105. // Username.
  106. private $username = "root";
  107.  
  108. // Password.
  109. private $password = ""; // This is a random password!
  110.  
  111. // Database charset.
  112. private $charset = "UTF8";
  113.  
  114. // PDO options.
  115. private $options = [
  116. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  117. PDO::ATTR_EMULATE_PREPARES => false,
  118. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  119. PDO::ATTR_PERSISTENT => true
  120. ];
  121.  
  122. /**
  123. *
  124. * Constructor.
  125. *
  126. * Creates connection to the database server.
  127. *
  128. **/
  129.  
  130. public function __construct() {
  131.  
  132. if ($this->connected === true) {
  133.  
  134. return true;
  135.  
  136.  
  137. } else {
  138.  
  139. try {
  140.  
  141. $this->connection = new PDO("mysql:host={$this->host};dbname={$this->database};charset={$this->charset}", $this->username, $this->password, $this->options);
  142. $this->connected = true;
  143.  
  144. } catch (PDOException $e) {
  145.  
  146. $this->error = $e->getMessage();
  147.  
  148. return null;
  149.  
  150. }
  151.  
  152. }
  153.  
  154. }
  155.  
  156. /**
  157. *
  158. * Query the Database.
  159. *
  160. * Used for SELECT, INSERT, UPDATE and DELETE statements.
  161. *
  162. **/
  163.  
  164. public function query($query, $parameters = [], $expectSingleResult = false) {
  165.  
  166. if ($this->connected === true) {
  167.  
  168. if (is_string($query) && $query !== "" && is_array($parameters) && is_bool($expectSingleResult)) {
  169.  
  170. try {
  171.  
  172. // Prepare SQL querey.
  173. $this->querey = $this->connection->prepare($query);
  174.  
  175. // Bind parameters to SQL querey.
  176. foreach ($parameters as $placeholder => $value) {
  177.  
  178. // Parameter type.
  179. if (is_string($value)) {
  180.  
  181. // Parameter is a string.
  182. $type = PDO::PARAM_STR;
  183.  
  184. } elseif (is_int($value)) {
  185.  
  186. // Parameter is a integer.
  187. $type = PDO::PARAM_INT;
  188.  
  189. } elseif (is_bool($value)) {
  190.  
  191. // Parameter is a boolean.
  192. $type = PDO::PARAM_BOOL;
  193.  
  194. } else {
  195.  
  196. // Parameter is NULL.
  197. $type = PDO::PARAM_NULL;
  198.  
  199. }
  200.  
  201. // Bind parameter.
  202. $this->querey->bindValue($placeholder, $value, $type);
  203.  
  204. }
  205.  
  206. // Execute SQL querey.
  207. $this->querey->execute();
  208.  
  209. // Get Result of SQL querey.
  210. if ($expectSingleResult === true) {
  211.  
  212. $results = $this->querey->fetch();
  213.  
  214. } else {
  215.  
  216. $results = $this->querey->fetchAll();
  217.  
  218. }
  219.  
  220. // Return results of SQL querey.
  221. return $results;
  222.  
  223. } catch (PDOException $e) {
  224.  
  225. $this->error = $e->getMessage();
  226.  
  227. }
  228.  
  229. } else {
  230.  
  231. $this->error = "Invalid Querey or Paramaters";
  232.  
  233. return null;
  234.  
  235. }
  236.  
  237. } else {
  238.  
  239. $this->error = "Not Connected to Database Server";
  240.  
  241. return null;
  242.  
  243. }
  244.  
  245. }
  246.  
  247. /**
  248. *
  249. * Row count for the last querey.
  250. *
  251. **/
  252.  
  253. public function rowCount() {
  254.  
  255. if ($this->connected === true) {
  256.  
  257. return $this->querey->rowCount();
  258.  
  259. } else {
  260.  
  261. $this->error = "Not Connected to Database Server";
  262.  
  263. return null;
  264.  
  265. }
  266.  
  267. }
  268.  
  269. /**
  270. *
  271. * Get ID for the last querey.
  272. *
  273. **/
  274.  
  275. public function lastId() {
  276.  
  277. if ($this->connected === true) {
  278.  
  279. return $this->connection->lastInsertId();
  280.  
  281. } else {
  282.  
  283. $this->error = "Not Connected to Database Server";
  284.  
  285. return null;
  286.  
  287. }
  288.  
  289. }
  290.  
  291. /**
  292. *
  293. * Begin a transaction.
  294. *
  295. **/
  296.  
  297. public function beginTransaction() {
  298.  
  299. if ($this->connected === true) {
  300.  
  301. return $this->connection->beginTransaction();
  302.  
  303. } else {
  304.  
  305. $this->error = "Not Connected to Database Server";
  306.  
  307. return null;
  308.  
  309. }
  310.  
  311. }
  312.  
  313. /**
  314. *
  315. * Rollback and cancel/end a transaction.
  316. *
  317. **/
  318.  
  319. public function cancelTransaction() {
  320.  
  321. if ($this->connected === true) {
  322.  
  323. return $this->connection->rollBack();
  324.  
  325. } else {
  326.  
  327. $this->error = "Not Connected to Database Server";
  328.  
  329. return null;
  330.  
  331. }
  332.  
  333. }
  334.  
  335. /**
  336. *
  337. * Or...
  338. *
  339. **/
  340.  
  341. public function rollbackTransaction() {
  342.  
  343. if ($this->connected === true) {
  344.  
  345. return $this->connection->rollBack();
  346.  
  347. } else {
  348.  
  349. $this->error = "Not Connected to Database Server";
  350.  
  351. return null;
  352.  
  353. }
  354.  
  355. }
  356.  
  357. /**
  358. *
  359. * Commit and end a transaction.
  360. *
  361. **/
  362.  
  363. public function endTransaction() {
  364.  
  365. if ($this->connected === true) {
  366.  
  367. return $this->connection->commit();
  368.  
  369. } else {
  370.  
  371. $this->error = "Not Connected to Database Server";
  372.  
  373. return null;
  374.  
  375. }
  376.  
  377. }
  378.  
  379. /**
  380. *
  381. * Close the current connection the the database server.
  382. *
  383. **/
  384.  
  385. public function close() {
  386.  
  387. $this->connection = null;
  388.  
  389. }
  390.  
  391. }
  392.  
  393. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement