Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MyPDO extends PDO
- {
- public function __construct($dsn, $username = NULL, $password = NULL, $options = [])
- {
- $default_options = [
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- ];
- $options = array_merge($default_options, $options);
- parent::__construct($dsn, $username, $password, $options);
- }
- public function run($sql, $args = NULL)
- {
- if (!$args)
- {
- return $this->query($sql);
- }
- $stmt = $this->prepare($sql);
- $stmt->execute($args);
- return $stmt;
- }
- }
- class MyPDO extends PDO
- {
- public function run($sql, $args = NULL)
- {
- if (!$args) {
- return $this->query($sql);
- }
- $stmt = $this->prepare($sql);
- $stmt->execute($args);
- return $stmt;
- }
- }
- //database connectie
- <!--//-->
- <!--//<!--//class Database {-->-->
- <!--//<!--// private $_connection;-->-->
- <!--//<!--// private static $_instance; //Enkel-->-->
- <!--//<!--// private $_host = "localhost";-->-->
- <!--//<!--// private $_username = "root";-->-->
- <!--//<!--// private $_password = "";-->-->
- <!--//<!--// private $_database = "url";-->-->
- <!--//<!--// /*-->-->
- <!--//<!--// Get an instance of the Database-->-->
- <!--//<!--// @return Instance-->-->
- <!--//<!--// */-->-->
- <!--//<!--// public static function getInstance() {-->-->
- <!--//<!--// if(!self::$_instance) { // Als er nog geen connectie bestaad-->-->
- <!--//<!--// self::$_instance = new self();-->-->
- <!--//<!--// }-->-->
- <!--//<!--// return self::$_instance;-->-->
- <!--//<!--// }-->-->
- <!--//<!--// // Constructor-->-->
- <!--//<!--// private function __construct() {-->-->
- <!--//<!--// $this->_connection = new mysqli($this->_host, $this->_username,-->-->
- <!--//<!--// $this->_password, $this->_database);-->-->
- <!--//<!--//-->-->
- <!--//<!--// // Error handling-->-->
- <!--//<!--// if(mysqli_connect_error()) {-->-->
- <!--//<!--// trigger_error("Fout bij het verbinden met MySQL: " . mysqli_connect_error(),-->-->
- <!--//<!--// E_USER_ERROR);-->-->
- <!--//<!--// }-->-->
- <!--//<!--// }-->-->
- <!--//<!--// // Magic method clone is empty to prevent duplication of connection-->-->
- <!--//<!--// private function __clone() { }-->-->
- <!--//<!--// // Get mysqli connection-->-->
- <!--//<!--// public function getConnection() {-->-->
- <!--//<!--// return $this->_connection;-->-->
- <!--//<!--// }-->-->
- <!--//<!--//}-->-->
- <!--//<!--//-->
- <?php
- if(stristr($_SERVER['REQUEST_URI'], 'connection.php')){
- die("Wait a minute... who are you? You're not allowed to come here.");
- }
- //TODO omzetten naar pdo
- class Database {
- // Connection information.
- private $connection;
- // SQL querey information.
- private $querey;
- // Connected to the database server.
- private $connected = false;
- // Errors.
- private $error;
- // Hostname or IP address of the database server.
- private $host = "localhost";
- // Name of the database.
- private $database = "url";
- // Username.
- private $username = "root";
- // Password.
- private $password = ""; // This is a random password!
- // Database charset.
- private $charset = "UTF8";
- // PDO options.
- private $options = [
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => false,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_PERSISTENT => true
- ];
- /**
- *
- * Constructor.
- *
- * Creates connection to the database server.
- *
- **/
- public function __construct() {
- if ($this->connected === true) {
- return true;
- } else {
- try {
- $this->connection = new PDO("mysql:host={$this->host};dbname={$this->database};charset={$this->charset}", $this->username, $this->password, $this->options);
- $this->connected = true;
- } catch (PDOException $e) {
- $this->error = $e->getMessage();
- return null;
- }
- }
- }
- /**
- *
- * Query the Database.
- *
- * Used for SELECT, INSERT, UPDATE and DELETE statements.
- *
- **/
- public function query($query, $parameters = [], $expectSingleResult = false) {
- if ($this->connected === true) {
- if (is_string($query) && $query !== "" && is_array($parameters) && is_bool($expectSingleResult)) {
- try {
- // Prepare SQL querey.
- $this->querey = $this->connection->prepare($query);
- // Bind parameters to SQL querey.
- foreach ($parameters as $placeholder => $value) {
- // Parameter type.
- if (is_string($value)) {
- // Parameter is a string.
- $type = PDO::PARAM_STR;
- } elseif (is_int($value)) {
- // Parameter is a integer.
- $type = PDO::PARAM_INT;
- } elseif (is_bool($value)) {
- // Parameter is a boolean.
- $type = PDO::PARAM_BOOL;
- } else {
- // Parameter is NULL.
- $type = PDO::PARAM_NULL;
- }
- // Bind parameter.
- $this->querey->bindValue($placeholder, $value, $type);
- }
- // Execute SQL querey.
- $this->querey->execute();
- // Get Result of SQL querey.
- if ($expectSingleResult === true) {
- $results = $this->querey->fetch();
- } else {
- $results = $this->querey->fetchAll();
- }
- // Return results of SQL querey.
- return $results;
- } catch (PDOException $e) {
- $this->error = $e->getMessage();
- }
- } else {
- $this->error = "Invalid Querey or Paramaters";
- return null;
- }
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Row count for the last querey.
- *
- **/
- public function rowCount() {
- if ($this->connected === true) {
- return $this->querey->rowCount();
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Get ID for the last querey.
- *
- **/
- public function lastId() {
- if ($this->connected === true) {
- return $this->connection->lastInsertId();
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Begin a transaction.
- *
- **/
- public function beginTransaction() {
- if ($this->connected === true) {
- return $this->connection->beginTransaction();
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Rollback and cancel/end a transaction.
- *
- **/
- public function cancelTransaction() {
- if ($this->connected === true) {
- return $this->connection->rollBack();
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Or...
- *
- **/
- public function rollbackTransaction() {
- if ($this->connected === true) {
- return $this->connection->rollBack();
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Commit and end a transaction.
- *
- **/
- public function endTransaction() {
- if ($this->connected === true) {
- return $this->connection->commit();
- } else {
- $this->error = "Not Connected to Database Server";
- return null;
- }
- }
- /**
- *
- * Close the current connection the the database server.
- *
- **/
- public function close() {
- $this->connection = null;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement