Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. <?php declare(strict_types = 1);
  2.  
  3. namespace CMS\Data;
  4. include_once(__DIR__ . '/../helpers/array.php');
  5.  
  6. /**
  7. * Database class that handles all database
  8. * connections and related actions.
  9. *
  10. * @param $user The connecting user
  11. * @param $pass Password for the $user
  12. * @param $database The database to connect to
  13. * @param $server The server the database is on
  14. * @param $port [Optional] Port number of the $server
  15. */
  16. class Database
  17. {
  18. public $connection;
  19. private $userName;
  20. private $password;
  21. private $databaseName;
  22. private $server;
  23. private $port;
  24. private $isConnected;
  25. private $connectionAttempts;
  26.  
  27. public function __construct($user, $pass, $database, $server, $port = "")
  28. {
  29. $this->userName = $user;
  30. $this->password = $pass;
  31. $this->databaseName = $database;
  32. $this->server = $server;
  33. $this->port = $port;
  34. $this->isConnected = false;
  35. $this->connectionAttempts = 0;
  36. }
  37.  
  38. /*
  39. Connects to the database via MySQLi and sets the
  40. local $isConnected variable.
  41. */
  42. public function connect()
  43. {
  44. if (!$this->isConnected)
  45. {
  46. $this->connection = new \mysqli($this->server, $this->userName, $this->password, $this->databaseName);
  47.  
  48. if ($this->connection->connect_errno)
  49. {
  50. echo "Sorry, something went wrong with your connection.\n";
  51. echo "Error: Failed to make a MySQL connection to the database: <strong>" . $this->databaseName . "</strong>\n";
  52. echo "Errno: " . $this->connection->connect_errno . "\n";
  53. echo "Error: " . $this->connection->connect_error;
  54. exit;
  55. }
  56.  
  57. $this->isConnected = true;
  58. }
  59. }
  60.  
  61. /**
  62. * Runs a select query and returns the associative array or false.
  63. *
  64. * @param $table A string of the table name
  65. * @param $columns An array of column names or a string value ('*', 'name', etc.)
  66. * @param $options An associative array of options (where, order by, like, etc.)
  67. * Ex: ['where' => 'this', '=' => 'that'],
  68. * ['where' => 'this', 'like' => 'that'],
  69. * ['order by' => 'this']
  70. * @param $andOrArray [Optional] An array with a collection of 'and' or 'or' used after each pair of $options
  71. */
  72. public function select($table, $columns, $options, $andOrArray = [])
  73. {
  74. if ($this->isConnected)
  75. {
  76. $index = 0;
  77. $query = "SELECT ";
  78. $query .= $this->buildSelectColumns($query, $table, $columns, $numColumns, $index);
  79.  
  80. foreach ($options as $key => $value)
  81. {
  82. $query = $this->buildSelectOptions($query, $key, $value);
  83. }
  84.  
  85. echo $query. "<br />";
  86.  
  87. if ($result = $this->query($query))
  88. {
  89. $sqlResults = $result->fetch_assoc();
  90. $result->free();
  91. return $sqlResults;
  92. }
  93. else
  94. {
  95. return false;
  96. }
  97.  
  98. $this->connection->close();
  99. }
  100. else
  101. {
  102. $this->connect();
  103. }
  104. }
  105.  
  106. /**
  107. * Runs an insert query and returns the insert id
  108. *
  109. * @param $table A string of the table name
  110. * @param $columns An array of the columns names
  111. * @param $values An array of the inserted values
  112. */
  113. public function insert($table, $columns, $values)
  114. {
  115. if ($this->isConnected)
  116. {
  117. $numColumns = \count($columns);
  118. $numValues = \count($values);
  119. $index = 0;
  120.  
  121. $query = "INSERT INTO `" . $table . "` ";
  122. $query .= "(";
  123.  
  124. foreach ($columns as $c)
  125. {
  126. $query .= $c;
  127.  
  128. if (++$index !== $numColumns)
  129. {
  130. $query .= ", ";
  131. }
  132. }
  133.  
  134. $index = 0;
  135. $query .= ") VALUES (";
  136.  
  137. foreach ($values as $v)
  138. {
  139. if (\is_string($v))
  140. {
  141. $query .= "'" . $v . "'";
  142. }
  143. else
  144. {
  145. $query .= $v;
  146. }
  147.  
  148. if (++$index !== $numValues)
  149. {
  150. $query .= ", ";
  151. }
  152. }
  153.  
  154. $query .= ")";
  155.  
  156. $this->query($query);
  157. return $this->connection->insert_id;
  158. }
  159. else
  160. {
  161. $this->connect();
  162. }
  163. }
  164.  
  165. /*
  166. Returns if there is a connection to the database
  167. */
  168. public function getConnection()
  169. {
  170. return $this->isConnected;
  171. }
  172.  
  173. /**
  174. * Runs a SQL query and returns the result.
  175. * Only attempts to connect 3 times before failing.
  176. *
  177. * @param $sql The SQL statement to run a query on
  178. */
  179. private function query($sql)
  180. {
  181. if ($this->isConnected)
  182. {
  183. return $this->connection->query($sql);
  184. }
  185. else
  186. {
  187. if ($this->connectionAttempts < 3)
  188. {
  189. $this->connect();
  190. $this->query($sql);
  191. $this->connectionAttempts++;
  192. }
  193. else
  194. {
  195. die("Cannot connect to database: <strong>" . $this->databaseName . "</strong>. Please check your connection information.");
  196. }
  197. }
  198. }
  199.  
  200. /*
  201. Helper function to build the columns query for
  202. the select function.
  203. */
  204. private function buildSelectColumns(&$query, $table, $columns, $index)
  205. {
  206. $numColumns = \count($columns);
  207. $isString = \is_string($columns);
  208.  
  209. if ($isString && $columns == "*")
  210. {
  211. $query .= "* FROM `" . $table . "` ";
  212.  
  213. }
  214. elseif ($isString)
  215. {
  216. $query .= $columns;
  217.  
  218. if (++$index !== $numColumns)
  219. {
  220. $query .= ", ";
  221. }
  222. else
  223. {
  224. " FROM `" . $table . "` ";
  225. }
  226. }
  227. else
  228. {
  229. foreach ($columns as $c)
  230. {
  231. if (\is_string($c))
  232. {
  233. $query .= "'" . $c . "'";
  234. }
  235. else
  236. {
  237. $query .= $c;
  238. }
  239.  
  240. if (++$index !== $numColumns)
  241. {
  242. $query .= ", ";
  243. }
  244. }
  245. }
  246. }
  247.  
  248. /*
  249. Helper function to build the options query for
  250. the select function.
  251. */
  252. private function buildSelectOptions(&$query, $key, $value)
  253. {
  254. $index = 0;
  255. $valCount = \count($value);
  256. $query .= \strtoupper($key) . " ";
  257.  
  258. foreach ($value as $v)
  259. {
  260. if (++$index !== $valCount)
  261. {
  262. $query .= $v . " ";
  263. }
  264. else
  265. {
  266. if (\strcasecmp($v, "like") == 0)
  267. {
  268. $query .= "'%" . $v . "%'";
  269. }
  270. else
  271. {
  272. $query .= "'" . $v . "'";
  273. }
  274. }
  275. }
  276.  
  277. return $query;
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement