Created
October 22, 2013 18:51
-
-
Save JABirchall/7106035 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Database Class */ | |
class database { | |
/* Set Private Database info so only this class can connect to it */ | |
private $hostname; | |
private $database; | |
private $username; | |
private $password; | |
/* Other Variables */ | |
private $pdo; | |
/* Auto Load Database */ | |
function __construct($hostname, $database, $username, $password) { | |
/* Set Private Database values */ | |
$this->hostname = $hostname; | |
$this->port = 3306; | |
$this->database = $database; | |
$this->username = $username; | |
$this->password = $password; | |
/* Try to connect else catch the failure */ | |
try { | |
$this->pdo = new PDO("mysql:host={$this->hostname};port={$this->port};dbname={$this->database}", $this->username, $this->password, array(PDO::ATTR_PERSISTENT => true)); | |
} catch(PDOException $e) { | |
print "<b>Error - Connection Failed: </b>" . $e->getMessage() . "<br/>"; | |
die(); | |
} | |
} | |
/* Build Query based on $query variable */ | |
/* Example of Bind array(":id" => "1", ":soemthing" => "The value") */ | |
public function query($query, $bind = null) { | |
global $pdo; | |
$this->pdo->beginTransaction(); | |
/* Prepare Statment */ | |
$this->statement = $this->pdo->prepare($query); | |
/* Execute Query */ | |
$this->statement->execute($bind); | |
$this->pdo->commit(); | |
$this->insertid = $this->pdo->lastInsertId(); | |
} | |
/* Return row Count */ | |
public function count() { | |
/* Return Count */ | |
$result = $this->statement->rowCount(); | |
return $result; | |
} | |
/* return last id of paste */ | |
public function lastid() { | |
$result = $this->pdo->lastInsertId(); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment