cgm225 Posted May 16, 2008 Share Posted May 16, 2008 I wanted to get some feedback on a notes/blog entry class I have written to display posts stored in an MySQL database. What can I improve? What would you do differently? I am VERY new to OOP. class Notes { private $connection; private $id; public function __construct(mysqli $connection) { $this->connection = $connection; } public function setLimits($skip = 0, $numberRows = 10000) { $this->skip = $skip; $this->numberRows = $numberRows; } public function getEntries() { $query = "SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC LIMIT ?, ?"; $statement = $this->connection->prepare($query); $statement->bind_param('is', $this->skip, $this->numberRows); $statement->bind_result($id, $title, $date, $note, $timestamp); $statement->execute(); while($statement->fetch()) { $this->data[$id] = array( //Creating two dimensional array 'id' => $id, 'title' => $title, 'date' => $date, 'note' => $note, 'timestamp' => $timestamp ); } } public function getEntry($id) { $query = "SELECT id, title, date, note, timestamp FROM notes WHERE id = ?"; $statement = $this->connection->prepare($query); $statement->bind_param('s', $id); $statement->bind_result($id, $title, $date, $note, $timestamp); $statement->execute(); if($statement->fetch()) { $this->data = array( 'id' => $id, 'title' => $title, 'date' => $date, 'note' => $note, 'timestamp' => $timestamp ); } } public function getData() { return $this->data; } } //////EXAMPLE OF USAGE/////// $mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD); $mysqli->select_db('example_db'); $notes = new Notes($mysqli); $notes->setLimits(0,2); //example limits $notes->getEntries(); $data = $notes->getData(); foreach($data as $note) { echo htmlentities($note['id']) . "<br>"; echo htmlentities($note['title']) . "<br>"; echo htmlentities($note['date']) . "<br>"; echo stripslashes($note['note']) . "<br>"; echo htmlentities($note['timestamp']) . "<br>"; } $notes->getEntry('example_id'); $data = $notes->getData(); foreach($data as $note) { echo htmlentities($data['id']) . "<br>"; echo htmlentities($data['title']) . "<br>"; echo htmlentities($data['date']) . "<br>"; echo stripslashes($data['note']) . "<br>"; echo htmlentities($data['timestamp']) . "<br>"; } $mysqli->close(); Link to comment https://forums.phpfreaks.com/topic/105923-feedback-on-notesblog-entry-class/ Share on other sites More sharing options...
cgm225 Posted May 17, 2008 Author Share Posted May 17, 2008 bump Link to comment https://forums.phpfreaks.com/topic/105923-feedback-on-notesblog-entry-class/#findComment-543619 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Why are you using prepared statements when you're only running the query once per page, probably? Link to comment https://forums.phpfreaks.com/topic/105923-feedback-on-notesblog-entry-class/#findComment-543622 Share on other sites More sharing options...
cgm225 Posted May 17, 2008 Author Share Posted May 17, 2008 I decided to use them after reading this article: http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html No good? Link to comment https://forums.phpfreaks.com/topic/105923-feedback-on-notesblog-entry-class/#findComment-543638 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 It's fine, but pretty unnecessary for one query. Link to comment https://forums.phpfreaks.com/topic/105923-feedback-on-notesblog-entry-class/#findComment-543649 Share on other sites More sharing options...
cgm225 Posted May 17, 2008 Author Share Posted May 17, 2008 Thanks for the feedback? Any other constructive criticism? Link to comment https://forums.phpfreaks.com/topic/105923-feedback-on-notesblog-entry-class/#findComment-543650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.