So I'm trying to create an AJAX chat system. I've gotten to the point where I should be able to print_r(new Chat->fetchMessages()); the dummy data I've put into the db table but for some reason it's not coming up. I've searched for any errors in the code and looked at the error logs on the server and haven't come up with anything. Please have a look.
Thank you in advance!!
index.php:
<?php error_reporting(E_ALL ^ E_NOTICE); session_start(); $userid = $_SESSION['userid']; $username = $_SESSION['username']; echo $userid; echo "|"; echo $username; echo "|"; require 'core/classes/Core.php'; require 'core/classes/Chat.php'; $chat = new Chat(); print_r($chat->fetchMessages()); echo mysql_error(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>AJAX Chat</title> <link href="./style.css" rel="stylesheet" type="text/css" /> </head> <body> <!-- Chat Start --> <div class="chat"> <div class="messages"> </div> <textarea class="entry" placeholder="Type here and hit Enter. Use Shift + Enter for a new line."></textarea> </div> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="./chat.js"></script> </body> </html>
Core.php and Chat.php within ./core/classes/:
Core.php:
<?php class Core { protected $db, $result; private $rows; public function __construct() { $this->db = new mysqli('localhost', 'root', 'db_password', 'db_name'); } public function query($sql) { $this->result = $this->db->query($sql); } public function rows() { for ($x = 1; $x <= $this->db->affected_rows; $x++) { $this->rows[] = $this->result->fetch_assoc(); } return $this->rows; } }
Chat.php:
<?php class Chat extends Core { public function fetchMessages() { $this->query("SELECT `chat`.`message`, `users`.`username`, `users`.`id` FROM `chat` JOIN `users` ON `chat.`user_id` = `users`.`id` ORDER BY `chat`.`timestamp` DESC"); return $this->rows(); } public function throwMessages($user_id, $message) { //insert into db } }
Let me know if any other information would be helpful. Thank you!!!