Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // This is here to prevent remote access to this file
- if(!defined('LOCK')) {
- die("Access Denied!");
- }
- // This is here to gather database connection information
- require('config.php');
- // This is where the magic happens
- class Database {
- // Connect to the database using PDO which is more secure than MySQL and MySQLi
- function connect($host,$dbname,$port,$charset,$username,$password) {
- $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
- $opt = [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- PDO::ATTR_EMULATE_PREPARES => FALSE
- ];
- return new PDO($dsn,$username,$password,$opt);
- }
- // Send data to the database
- function sendData($pdo,$query,$arr) {
- $stmt = $pdo->prepare($query);
- $stmt->execute($arr);
- return 1;
- }
- // Gather data from the database
- function getData($pdo,$query,$arr) {
- $stmt = $pdo->prepare($query);
- $stmt->execute($arr);
- $data = $stmt->fetchColumn();
- return $data;
- }
- // Null the database connection to "close" the connection
- function close($pdo) {
- if(isset($pdo)) {
- $pdo = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement