Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php declare(strict_types = 1);
- namespace CMS\Data;
- include_once(__DIR__ . '/../helpers/array.php');
- /**
- * Database class that handles all database
- * connections and related actions.
- *
- * @param $user The connecting user
- * @param $pass Password for the $user
- * @param $database The database to connect to
- * @param $server The server the database is on
- * @param $port [Optional] Port number of the $server
- */
- class Database
- {
- public $connection;
- private $userName;
- private $password;
- private $databaseName;
- private $server;
- private $port;
- private $isConnected;
- private $connectionAttempts;
- public function __construct($user, $pass, $database, $server, $port = "")
- {
- $this->userName = $user;
- $this->password = $pass;
- $this->databaseName = $database;
- $this->server = $server;
- $this->port = $port;
- $this->isConnected = false;
- $this->connectionAttempts = 0;
- }
- /*
- Connects to the database via MySQLi and sets the
- local $isConnected variable.
- */
- public function connect()
- {
- if (!$this->isConnected)
- {
- $this->connection = new \mysqli($this->server, $this->userName, $this->password, $this->databaseName);
- if ($this->connection->connect_errno)
- {
- echo "Sorry, something went wrong with your connection.\n";
- echo "Error: Failed to make a MySQL connection to the database: <strong>" . $this->databaseName . "</strong>\n";
- echo "Errno: " . $this->connection->connect_errno . "\n";
- echo "Error: " . $this->connection->connect_error;
- exit;
- }
- $this->isConnected = true;
- }
- }
- /**
- * Runs a select query and returns the associative array or false.
- *
- * @param $table A string of the table name
- * @param $columns An array of column names or a string value ('*', 'name', etc.)
- * @param $options An associative array of options (where, order by, like, etc.)
- * Ex: ['where' => 'this', '=' => 'that'],
- * ['where' => 'this', 'like' => 'that'],
- * ['order by' => 'this']
- * @param $andOrArray [Optional] An array with a collection of 'and' or 'or' used after each pair of $options
- */
- public function select($table, $columns, $options, $andOrArray = [])
- {
- if ($this->isConnected)
- {
- $index = 0;
- $query = "SELECT ";
- $query .= $this->buildSelectColumns($query, $table, $columns, $numColumns, $index);
- foreach ($options as $key => $value)
- {
- $query = $this->buildSelectOptions($query, $key, $value);
- }
- echo $query. "<br />";
- if ($result = $this->query($query))
- {
- $sqlResults = $result->fetch_assoc();
- $result->free();
- return $sqlResults;
- }
- else
- {
- return false;
- }
- $this->connection->close();
- }
- else
- {
- $this->connect();
- }
- }
- /**
- * Runs an insert query and returns the insert id
- *
- * @param $table A string of the table name
- * @param $columns An array of the columns names
- * @param $values An array of the inserted values
- */
- public function insert($table, $columns, $values)
- {
- if ($this->isConnected)
- {
- $numColumns = \count($columns);
- $numValues = \count($values);
- $index = 0;
- $query = "INSERT INTO `" . $table . "` ";
- $query .= "(";
- foreach ($columns as $c)
- {
- $query .= $c;
- if (++$index !== $numColumns)
- {
- $query .= ", ";
- }
- }
- $index = 0;
- $query .= ") VALUES (";
- foreach ($values as $v)
- {
- if (\is_string($v))
- {
- $query .= "'" . $v . "'";
- }
- else
- {
- $query .= $v;
- }
- if (++$index !== $numValues)
- {
- $query .= ", ";
- }
- }
- $query .= ")";
- $this->query($query);
- return $this->connection->insert_id;
- }
- else
- {
- $this->connect();
- }
- }
- /*
- Returns if there is a connection to the database
- */
- public function getConnection()
- {
- return $this->isConnected;
- }
- /**
- * Runs a SQL query and returns the result.
- * Only attempts to connect 3 times before failing.
- *
- * @param $sql The SQL statement to run a query on
- */
- private function query($sql)
- {
- if ($this->isConnected)
- {
- return $this->connection->query($sql);
- }
- else
- {
- if ($this->connectionAttempts < 3)
- {
- $this->connect();
- $this->query($sql);
- $this->connectionAttempts++;
- }
- else
- {
- die("Cannot connect to database: <strong>" . $this->databaseName . "</strong>. Please check your connection information.");
- }
- }
- }
- /*
- Helper function to build the columns query for
- the select function.
- */
- private function buildSelectColumns(&$query, $table, $columns, $index)
- {
- $numColumns = \count($columns);
- $isString = \is_string($columns);
- if ($isString && $columns == "*")
- {
- $query .= "* FROM `" . $table . "` ";
- }
- elseif ($isString)
- {
- $query .= $columns;
- if (++$index !== $numColumns)
- {
- $query .= ", ";
- }
- else
- {
- " FROM `" . $table . "` ";
- }
- }
- else
- {
- foreach ($columns as $c)
- {
- if (\is_string($c))
- {
- $query .= "'" . $c . "'";
- }
- else
- {
- $query .= $c;
- }
- if (++$index !== $numColumns)
- {
- $query .= ", ";
- }
- }
- }
- }
- /*
- Helper function to build the options query for
- the select function.
- */
- private function buildSelectOptions(&$query, $key, $value)
- {
- $index = 0;
- $valCount = \count($value);
- $query .= \strtoupper($key) . " ";
- foreach ($value as $v)
- {
- if (++$index !== $valCount)
- {
- $query .= $v . " ";
- }
- else
- {
- if (\strcasecmp($v, "like") == 0)
- {
- $query .= "'%" . $v . "%'";
- }
- else
- {
- $query .= "'" . $v . "'";
- }
- }
- }
- return $query;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement