Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include 'oPost.php';
- /**
- * @author Matt Robineau
- *
- */
- class dbDAL {
- private $mysql;
- private $query;
- /**
- *
- */
- public function __construct() {
- $this->mysql = new mysqli("127.0.0.1", "root", "yhbtYTMND", "mldb") OR die("Database connection error.");
- }
- /**
- *
- */
- public function __destroy() {
- if($mysql->ping())
- $this->mysql->close();
- }
- // Return raw query result.
- private function execute($q) {
- if($mysql->ping()) {
- $result = $this->mysql->query($q, MYSQLI_STORE_RESULT);
- return $result;
- }
- }
- public function getAll() {
- }
- public function getByID(int $id) {
- if($mysql->ping()) {
- $q = "SELECT * FROM posts WHERE id = " + $id;
- return execute($q);
- }
- // If fetch_object is EOL, it will send a NULL result, so if !connected then result is NULL
- return NULL;
- }
- /**
- * @param int $year
- * @param int $month
- * @return <NULL, oPost>
- * Returns an array of oPost or null if no post exists for year/month.
- */
- public function selectMonthYear(int $month, int $year) {
- $data = null;
- if($mysql->ping()) {
- $q= "SELECT * FROM posts WHERE YEAR(date) = " + $year +
- " AND MONTH(date) = " + $month;
- $rows = execute($q);
- if($rows->num_rows > 0) {
- $c = 0;
- while($row = $rows->fetch_assoc()) {
- $p = new oPost();
- $p->populate($row);
- $data[$c] = $p;
- $c++;
- }
- }
- }
- return $data;
- }
- // Use mysql_real_escape_string() to sanitize input
- public function insert(oPost $p)
- {
- if($mysql->ping()) {
- $q = "INSERT INTO posts (date, title, post, summary, visible) VALUES (" +
- mysql_real_escape_string($p->date) +
- mysql_real_escape_string($p->title) +
- mysql_real_escape_string($p->article) +
- mysql_real_escape_string($p->summary) +
- mysql_real_escape_string($p->visible) +
- ")";
- return execute($q);
- }
- return false;
- }
- /**
- * @param int $id Delete's post from the database.
- *
- */
- public function delete(oPost $p)
- {
- if($mysql->ping())
- {
- $q = "DELETE FROM posts WHERE postID=" + mysql_real_escape_string($p->postID);
- return execute($q);
- }
- return false;
- }
- // $p: oPost object that requires an update.
- // Return: true if success, false if failed.
- public function update(oPost $p)
- {
- if($mysql->ping()) {
- $q = "UPDATE posts SET" +
- " date=" + mysql_real_escape_string($p->date) +
- " title=" + mysql_real_escape_string($p->title) +
- " post=" + mysql_real_escape_string($p->article) +
- " summary=" + mysql_real_escape_string($p->summary) +
- " visible=" + mysql_real_escape_string($p->visible) +
- " WHERE postID=" + mysql_real_escape_string($p->postID);
- return execute($q);
- }
- return false;
- }
- public function close() {
- $this->mysql->close();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement