Advertisement
kchoman

TrinityCore Database Class

Feb 22nd, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. <?php
  2. // This is here to prevent remote access to this file
  3. if(!defined('LOCK')) {
  4.     die("Access Denied!");
  5. }
  6.  
  7. // This is here to gather database connection information
  8. require('config.php');
  9.  
  10. // This is where the magic happens
  11. class Database {
  12.     // Connect to the database using PDO which is more secure than MySQL and MySQLi
  13.     function connect($host,$dbname,$port,$charset,$username,$password) {
  14.         $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
  15.         $opt = [
  16.             PDO::ATTR_ERRMODE               => PDO::ERRMODE_EXCEPTION,
  17.             PDO::ATTR_DEFAULT_FETCH_MODE    => PDO::FETCH_ASSOC,
  18.             PDO::ATTR_EMULATE_PREPARES      => FALSE
  19.         ];
  20.         return new PDO($dsn,$username,$password,$opt);
  21.     }
  22.     // Send data to the database
  23.     function sendData($pdo,$query,$arr) {
  24.         $stmt = $pdo->prepare($query);
  25.         $stmt->execute($arr);
  26.         return 1;
  27.     }
  28.     // Gather data from the database
  29.     function getData($pdo,$query,$arr) {
  30.         $stmt = $pdo->prepare($query);
  31.         $stmt->execute($arr);
  32.         $data = $stmt->fetchColumn();
  33.         return $data;
  34.     }
  35.     // Null the database connection to "close" the connection
  36.     function close($pdo) {
  37.         if(isset($pdo)) {
  38.             $pdo = null;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement