Private Message System help!

Post here is you are having problems with any of the tutorials.
Locked
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Private Message System help!

Post by Shahlin »

I'm in the ending of the 9th part of the PM system. In the video (14:52 minute), Jacek's is shown keys and values of the array. But what I get is, just an empty array like this : Array()
Please help!

This is my fetch_conversation_summary function
function fetch_conversation_summary(){
	$sql = "SELECT
				 `conversations`.`conversation_id`,
				 `conversations`.`conversation_subject`,
				 MAX(`conversations_messages`.`message_date`) AS `conversation_last_reply`
			FROM `conversations`
			LEFT JOIN `conversations_messages` ON `conversations`.`conversation_id` = `conversations_messages`.`conversation_id`
			INNER JOIN `conversations_members` ON `conversations`.`conversation_id` = `conversations_members`.`conversation_id`
			WHERE `conversations_members`.`user_id` = {$_SESSION['user_id']}
			AND `conversations_members`.`conversation_deleted` = 0
			GROUP BY `conversations`.`conversation_id`
			ORDER BY `conversation_last_reply` DESC";
	
	$result = mysql_query($sql);
	
	$conversations = array();
	
	while(($row = mysql_fetch_assoc($result)) !== false){
		$conversations[] = array(
			'id'			=> $row['conversation_id'],
			'subject' 		=> $row['conversation_subject'],
			'last_reply' 	=> $row['conversation_last_reply']
		);
	}
	return $conversations;
}
This is my php part of inbox.inc.php page :
<?php

$conversations = fetch_conversation_summary();
print_r($conversations);
?>
Just A PHP Beginner!
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Private Message System help!

Post by ExtremeGaming »

It is my guess that your query is failing. Change return $conversations; to return mysql_error(); Then instead of print_r just echo
<?php while(!$succeed = try()); ?>
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Private Message System help!

Post by Shahlin »

I tried! But it didn't work!
Just A PHP Beginner!
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Private Message System help!

Post by ExtremeGaming »

I have no idea unless you aren't starting the session or are not logged in. You may have to wait for someone with better eyes than mine :?
<?php while(!$succeed = try()); ?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Private Message System help!

Post by jacek »

ExtremeGaming wrote:You may have to wait for someone with better eyes than mine :?
/me enters room

An empty array means that the query is technically working but not returning any rows, since it looks like the correct format that probably means the WHERE is faulty.
`conversations_members`.`conversation_deleted` = 0
is okay (as long as there are some rows in the table with a 1 here) so the problem must be in
WHERE `conversations_members`.`user_id` = {$_SESSION['user_id']}
which also looks fine. So the best guess is that $_SESSION['user_id'] does not contain the value that is expected. So back to
ExtremeGaming wrote:I have no idea unless you aren't starting the session or are not logged in.
Make sure you are including the init.inc.php file correctly, that you are logged in correctly and that you call session_start() in the init.inc.php file.

If would also be worth setting your error_reporting level to E_ALL since that would highlight this issue with a nice error message if it is the problem.
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Private Message System help!

Post by Shahlin »

jacek wrote:
ExtremeGaming wrote:You may have to wait for someone with better eyes than mine :?
/me enters room

An empty array means that the query is technically working but not returning any rows, since it looks like the correct format that probably means the WHERE is faulty.
.
.
.
.
.
If would also be worth setting your error_reporting level to E_ALL since that would highlight this issue with a nice error message if it is the problem.
I tried, doesn't work. :( I tried pasting the code on phpmyadmin and checking. But it says : MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0006 sec )

Now what? D:
I don't wanna stop in between.
Just A PHP Beginner!
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Private Message System help!

Post by ExtremeGaming »

jacek wrote: Make sure you are including the init.inc.php file correctly, that you are logged in correctly and that you call session_start() in the init.inc.php file.
Since it's returning empty, you also need to make sure that the $_SESSION['user_id'] is set and is being used inside the table. If you think it may be a problem with your query, you might want to try return $sql; instead of return $conversations;
<?php while(!$succeed = try()); ?>
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Private Message System help!

Post by Shahlin »

ExtremeGaming wrote:
jacek wrote: Make sure you are including the init.inc.php file correctly, that you are logged in correctly and that you call session_start() in the init.inc.php file.
Since it's returning empty, you also need to make sure that the $_SESSION['user_id'] is set and is being used inside the table. If you think it may be a problem with your query, you might want to try return $sql; instead of return $conversations;
$_SESSION['user_id'] is working! Because, when I tried to return $sql; instead, I got this query :
SELECT `conversations`.`conversation_id`, `conversations`.`conversation_subject`, MAX(`conversations_messages`.`message_date`) AS `conversation_last_reply` FROM `conversations` LEFT JOIN `conversations_messages` ON `conversations`.`conversation_id` = `conversations_messages`.`conversation_id` INNER JOIN `conversations_members` ON `conversations`.`conversation_id` = `conversations_members`.`conversation_id` WHERE `conversations_members`.`user_id` = 1 AND `conversations_members`.`conversation_deleted` = 0 GROUP BY `conversations`.`conversation_id` ORDER BY `conversation_last_reply` DESC
I don't know what the problem is. :c
Just A PHP Beginner!
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: Private Message System help!

Post by ExtremeGaming »

Are there any rows in the database where $_SESSION['user_id'] = 1?
<?php while(!$succeed = try()); ?>
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Private Message System help!

Post by Shahlin »

ExtremeGaming wrote:Are there any rows in the database where $_SESSION['user_id'] = 1?
In my 'conversations_messages' table, there are rows with the user_id = 1. But my conversations_members table is empty.
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Private Message System help!

Post by jacek »

Shahlin wrote:But my conversations_members table is empty.
That is not good :P Could you post the INSERT query where the users are added to this table ?
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Private Message System help!

Post by Shahlin »

jacek wrote:
Shahlin wrote:But my conversations_members table is empty.
That is not good :P Could you post the INSERT query where the users are added to this table ?
This one?
$sql = "INSERT INTO `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`)
		VALUES({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), '{$body}')";
	
	mysql_query($sql);
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Private Message System help!

Post by jacek »

Is the table conversations_messages I think mine was conversation_messages, either way try sticking a
echo mysql_error();
after that mysql_query line.
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

Re: Private Message System help!

Post by Shahlin »

jacek wrote:Is the table conversations_messages I think mine was conversation_messages, either way try sticking a
echo mysql_error();
after that mysql_query line.
I have actually found out the mistake! :D I didn't add 's' in 'conversation' in one of the INSERT queries. Now, it works fine! :D
Just A PHP Beginner!
Locked