Advertisement
Guest User

Untitled

a guest
Mar 5th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. /**
  2. *
  3. * Database.
  4. *
  5. * Version 1.2 (March 2017).
  6. *
  7. **/
  8.  
  9. class Database {
  10.  
  11. // Connection information.
  12. private $connection;
  13.  
  14. // SQL querey information.
  15. private $querey;
  16.  
  17. // Connected to the database server.
  18. private $connected = false;
  19.  
  20. // Errors.
  21. private $error;
  22.  
  23. // Hostname or IP address of the database server.
  24. private $host = "127.0.0.1";
  25.  
  26. // Port to access the database server.
  27. private $port = 3306;
  28.  
  29. // Name of the database.
  30. private $database = "test";
  31.  
  32. // Username.
  33. private $username = "test";
  34.  
  35. // Password.
  36. private $password = "qgmxnMVroz9PHTY36Eq4qwpA5GSXwFyb";
  37.  
  38. // Database charset.
  39. private $charset = "UTF8";
  40.  
  41. // PDO options.
  42. private $options = [
  43. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  44. PDO::ATTR_EMULATE_PREPARES => false,
  45. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  46. PDO::ATTR_PERSISTENT => true
  47. ];
  48.  
  49. /**
  50. *
  51. * Constructor.
  52. *
  53. * Creates connection to the database server.
  54. *
  55. **/
  56.  
  57. public function __construct() {
  58.  
  59. if ($this->connected === true) {
  60.  
  61. return true;
  62.  
  63. } else {
  64.  
  65. try {
  66.  
  67. $this->connection = new PDO("mysql:host={$this->host};port={$this->port};dbname={$this->database};charset={$this->charset}", $this->username, $this->password, $this->options);
  68.  
  69. $this->connected = true;
  70.  
  71. } catch (PDOException $e) {
  72.  
  73. $this->error = $e->getMessage();
  74.  
  75. return null;
  76.  
  77. }
  78.  
  79. }
  80.  
  81. }
  82.  
  83. /**
  84. *
  85. * Query the Database.
  86. *
  87. * Used for SELECT, INSERT, UPDATE and DELETE statements.
  88. *
  89. **/
  90.  
  91. public function query($query, $parameters = [], $expectSingleResult = false) {
  92.  
  93. if ($this->connected === true) {
  94.  
  95. if (is_string($query) && $query !== "" && is_array($parameters) && is_bool($expectSingleResult)) {
  96.  
  97. try {
  98.  
  99. // Prepare SQL querey.
  100. $this->querey = $this->connection->prepare($query);
  101.  
  102. // Bind parameters to SQL querey.
  103. foreach ($parameters as $placeholder => $value) {
  104.  
  105. // Parameter type.
  106. if (is_string($value)) {
  107.  
  108. // Parameter is a string.
  109. $type = PDO::PARAM_STR;
  110.  
  111. } elseif (is_int($value)) {
  112.  
  113. // Parameter is a integer.
  114. $type = PDO::PARAM_INT;
  115.  
  116. } elseif (is_bool($value)) {
  117.  
  118. // Parameter is a boolean.
  119. $type = PDO::PARAM_BOOL;
  120.  
  121. } else {
  122.  
  123. // Parameter is NULL.
  124. $type = PDO::PARAM_NULL;
  125.  
  126. }
  127.  
  128. // Bind parameter.
  129. $this->querey->bindValue($placeholder, $value, $type);
  130.  
  131. }
  132.  
  133. // Execute SQL querey.
  134. $this->querey->execute();
  135.  
  136. // Get Result of SQL querey.
  137. if ($expectSingleResult === true) {
  138.  
  139. $results = $this->querey->fetch();
  140.  
  141. } else {
  142.  
  143. $results = $this->querey->fetchAll();
  144.  
  145. }
  146.  
  147. // Return results of SQL querey.
  148. return $results;
  149.  
  150. } catch (PDOException $e) {
  151.  
  152. $this->error = $e->getMessage();
  153.  
  154. }
  155.  
  156. } else {
  157.  
  158. $this->error = "Invalid Querey or Paramaters";
  159.  
  160. return null;
  161.  
  162. }
  163.  
  164. } else {
  165.  
  166. $this->error = "Not Connected to Database Server";
  167.  
  168. return null;
  169.  
  170. }
  171.  
  172. }
  173.  
  174. /**
  175. *
  176. * Row count for the last querey.
  177. *
  178. **/
  179.  
  180. public function rowCount() {
  181.  
  182. if ($this->connected === true) {
  183.  
  184. return $this->querey->rowCount();
  185.  
  186. } else {
  187.  
  188. $this->error = "Not Connected to Database Server";
  189.  
  190. return null;
  191.  
  192. }
  193.  
  194. }
  195.  
  196. /**
  197. *
  198. * Get ID for the last querey.
  199. *
  200. **/
  201.  
  202. public function lastId() {
  203.  
  204. if ($this->connected === true) {
  205.  
  206. return $this->connection->lastInsertId();
  207.  
  208. } else {
  209.  
  210. $this->error = "Not Connected to Database Server";
  211.  
  212. return null;
  213.  
  214. }
  215.  
  216. }
  217.  
  218. /**
  219. *
  220. * Begin a transaction.
  221. *
  222. **/
  223.  
  224. public function beginTransaction() {
  225.  
  226. if ($this->connected === true) {
  227.  
  228. return $this->connection->beginTransaction();
  229.  
  230. } else {
  231.  
  232. $this->error = "Not Connected to Database Server";
  233.  
  234. return null;
  235.  
  236. }
  237.  
  238. }
  239.  
  240. /**
  241. *
  242. * Rollback and cancel/end a transaction.
  243. *
  244. **/
  245.  
  246. public function cancelTransaction() {
  247.  
  248. if ($this->connected === true) {
  249.  
  250. return $this->connection->rollBack();
  251.  
  252. } else {
  253.  
  254. $this->error = "Not Connected to Database Server";
  255.  
  256. return null;
  257.  
  258. }
  259.  
  260. }
  261.  
  262. /**
  263. *
  264. * Or...
  265. *
  266. **/
  267.  
  268. public function rollbackTransaction() {
  269.  
  270. if ($this->connected === true) {
  271.  
  272. return $this->connection->rollBack();
  273.  
  274. } else {
  275.  
  276. $this->error = "Not Connected to Database Server";
  277.  
  278. return null;
  279.  
  280. }
  281.  
  282. }
  283.  
  284. /**
  285. *
  286. * Commit and end a transaction.
  287. *
  288. **/
  289.  
  290. public function endTransaction() {
  291.  
  292. if ($this->connected === true) {
  293.  
  294. return $this->connection->commit();
  295.  
  296. } else {
  297.  
  298. $this->error = "Not Connected to Database Server";
  299.  
  300. return null;
  301.  
  302. }
  303.  
  304. }
  305.  
  306. /**
  307. *
  308. * Close the current connection the the database server.
  309. *
  310. **/
  311.  
  312. public function close() {
  313.  
  314. $this->connection = null;
  315.  
  316. }
  317.  
  318. }
  319.  
  320. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement