Jump to content

Question about AJAX/PHP Chat system


Thaikhan

Recommended Posts

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!!!

Link to comment
Share on other sites

 

		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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.