Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
- defined('DB_USER') ? null : define("DB_USER", "root");
- defined('DB_PASS') ? null : define("DB_PASS", "*************");
- defined('DB_NAME') ? null : define("DB_NAME", "*************");
- ?>
- <?php
- class database extends mysqli {
- private $db;
- function __construct() {
- $this->db = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
- if ($this->db->connect_errno > 0) {
- die("Unable to connect to the database, throwing error: " . $db->connect_error);
- }
- }
- // performs a query, does a number of actions dependant on $type
- public function query($sql, $type = false) {
- $sql = $this->escape($sql);
- if ($result = $this->db->query($sql)) {
- if ($type == false) {
- return $result;
- } elseif ($type == true || "assoc") {
- if ($result->num_rows >= 2) {
- $array;
- $i = 1;
- while ($row = $result->fetch_assoc()) {
- $array[$i] = $row;
- $i++;
- }
- return $array;
- } elseif ($result->num_rows == 1) {
- return $result->fetch_assoc();
- }
- } elseif ($type == "array") {
- if ($result->num_rows >= 2) {
- $array;
- $i = 1;
- while ($row = $result->fetch_array()) {
- $array[$i] = $row;
- $i++;
- }
- return $array;
- } elseif ($result->num_rows == 1) {
- return $result->fetch_array();
- }
- }
- } else {
- die("There was an error running the query, throwing error: " . $this->db->error);
- }
- }
- // escapes a string before it is used in a query
- private function escape($string) {
- $string = $this->db->escape_string($string);
- return $string;
- }
- }
- $db = new database();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement