Thaikhan Posted August 19, 2013 Share Posted August 19, 2013 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!!! Quote Link to comment Share on other sites More sharing options...
kicken Posted August 19, 2013 Share Posted August 19, 2013 for ($x = 1; $x <= $this->db->affected_rows; $x++) { $this->rows[] = $this->result->fetch_assoc(); } return $this->rows; Rather than a for loop, use a while loop to read the results: $this->rows=array(); while ($row=$this->result->fetch_assoc()){ $this->rows[] = $row; } return $this->rows; Have you verified that your query actually returns data by running it in your db management tools? Quote Link to comment Share on other sites More sharing options...
Thaikhan Posted August 19, 2013 Author Share Posted August 19, 2013 I'm not certain how to do that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.