Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- private $host, $dbname, $user, $password, $connection = null;
- // Open database connection
- public function connect($host, $dbname, $user, $password) {
- $connectionString = sprintf("mysql:host=%s;dbname=%s;charset=utf8mb4", $host, $dbname);
- try {
- $this->connection = new PDO($connectionString, $user, $password);
- $this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->connection->query("SET sql_mode = ''");
- } catch (PDOException $pe) {
- die("Database error: ".$pe->getMessage());
- }
- }
- public function get_board_threads() {
- global $cfg;
- $query = "
- SELECT posts.id, posts.thread, posts.date, posts.header, posts.text
- FROM posts
- WHERE posts.thread = 0
- GROUP BY posts.id
- ORDER BY posts.last_reply DESC";
- $q = $this->connection->prepare($query);
- $q->execute();
- return $q->fetchAll();
- }
- // Working function but not what I want
- public function get_thread_latests_replies($id) {
- global $cfg;
- $query = "
- SELECT posts.id, posts.thread, posts.date, posts.header, posts.text
- FROM posts
- WHERE posts.thread = :thread
- GROUP BY posts.id
- ORDER BY posts.id ASC LIMIT 3";
- $q = $this->connection->prepare($query);
- $q->execute(array(":thread" => $id));
- return $q->fetchAll();
- }
- // Error is created here
- public function get_thread_latests_replies_error($id) {
- global $cfg;
- $query = "
- (SELECT posts.id, posts.thread, posts.date, posts.header, posts.text
- FROM posts
- WHERE posts.thread = :thread
- GROUP BY posts.id
- ORDER BY posts.id ASC LIMIT 3) ORDER BY id DESC";
- $q = $this->connection->prepare($query);
- $q->execute(array(":thread" => $id));
- return $q->fetchAll();
- }
- // Close the database connection
- public function __destruct() {
- $this->connection = null;
- }
- }
- /* Some "cosmetic" functions */
- function print_thread($thread) {
- $thread_html = "<h3>".$thread[0]["header"]."</h3>";
- $thread_html .= print_post($thread[0], true)."<div class='answers'>";
- for ($i = 1; $i < count($thread); $i++) {
- $thread_html .= print_post($thread[$i]);
- }
- $thread_html .= "
- </div>
- </div>";
- return $thread_html;
- }
- function print_post($post, $start_post = false) {
- $html = "
- <div style='border: 1px solid #000; margin: 3px;'>
- <div>#".$post["id"]." | ".$post["date"]."</div>
- <div>
- <p>".nl2br($post["text"])."</p>
- </div>";
- if ($start_post == false) {
- $html .= "</div>";
- }
- return $html;
- }
- /* End of "cosmetic" functions */
- echo "<h2>Run this SQL query in your MySQL Server first as root user</h2>
- <pre>
- CREATE DATABASE IF NOT EXISTS test_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
- CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'safepw123';
- GRANT ALL ON `test_db`.* TO 'test_user'@'localhost';
- FLUSH PRIVILEGES;
- USE test_db;
- CREATE TABLE `posts` (
- `id` int(11) NOT NULL,
- `thread` int(11) NOT NULL DEFAULT '0',
- `date` datetime NOT NULL,
- `last_reply` datetime DEFAULT NULL,
- `header` varchar(32) NOT NULL DEFAULT '',
- `text` varchar(8000) NOT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- INSERT INTO `posts` (`id`, `thread`, `date`, `last_reply`, `header`, `text`) VALUES
- (1, 0, '2016-04-17 14:31:28', '2016-04-17 15:52:33', 'Title', 'start post (always top/first)\r\n\r\nThe order should be 6, 7, 8 from top to bottom. So only the latest three messages should be showing'),
- (2, 1, '2016-04-17 14:31:56', NULL, '', '2nd post'),
- (3, 1, '2016-04-17 14:51:31', NULL, '', '3rd post'),
- (4, 1, '2016-04-17 14:55:42', NULL, '', '4th post'),
- (5, 1, '2016-04-17 15:34:01', NULL, '', '5th post'),
- (6, 1, '2016-04-17 15:43:44', NULL, '', '6th posts'),
- (7, 1, '2016-04-17 15:49:12', NULL, '', '7th posts'),
- (8, 1, '2016-04-17 15:52:33', NULL, '', '8th posts');
- ALTER TABLE `posts`
- ADD UNIQUE KEY `id` (`id`),
- ADD KEY `thread` (`thread`);
- </pre>";
- echo "<br><br><h2 style='color:red;'>To create a PHP error add \"?error=1\" in the url</h2>";
- echo "<br><br><h2>PHP Output:</h2>";
- /* Connect to database */
- $db = new Database();
- $db->connect("127.0.0.1", "test_db", "test_user", "safepw123");
- $threads = $db->get_board_threads();
- $threads_html = "";
- foreach ($threads as $thread) {
- if (isset($_GET["error"]) AND $_GET["error"] == 1) {
- // Creates error
- $replies = $db->get_thread_latests_replies_error($thread["id"]);
- } else {
- // Doesn't create error
- $replies = $db->get_thread_latests_replies($thread["id"]);
- }
- $threads_html .= print_thread(array_merge(array($thread), $replies));
- }
- // Print the output
- echo "<div style='width: 300px;'>".$threads_html."</div>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement