Page 1 of 1

Private Message System help!

Posted: Wed Dec 26, 2012 11:38 am
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
[syntax=php]
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;
}
[/syntax]

This is my php part of inbox.inc.php page :
[syntax=php]
<?php

$conversations = fetch_conversation_summary();
print_r($conversations);
?>
[/syntax]

Re: Private Message System help!

Posted: Wed Dec 26, 2012 5:24 pm
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

Re: Private Message System help!

Posted: Wed Dec 26, 2012 7:35 pm
by Shahlin
I tried! But it didn't work!

Re: Private Message System help!

Posted: Wed Dec 26, 2012 7:48 pm
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 :?

Re: Private Message System help!

Posted: Sat Dec 29, 2012 2:10 am
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.

[syntax=sql]`conversations_members`.`conversation_deleted` = 0[/syntax]
is okay (as long as there are some rows in the table with a 1 here) so the problem must be in

[syntax=sql]WHERE `conversations_members`.`user_id` = {$_SESSION['user_id']}[/syntax]
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.

Re: Private Message System help!

Posted: Sat Dec 29, 2012 2:46 pm
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.

Re: Private Message System help!

Posted: Sat Dec 29, 2012 5:47 pm
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;

Re: Private Message System help!

Posted: Sun Dec 30, 2012 9:31 am
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 :

[syntax=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` = 1 AND `conversations_members`.`conversation_deleted` = 0 GROUP BY `conversations`.`conversation_id` ORDER BY `conversation_last_reply` DESC[/syntax]

I don't know what the problem is. :c

Re: Private Message System help!

Posted: Sun Dec 30, 2012 7:13 pm
by ExtremeGaming
Are there any rows in the database where $_SESSION['user_id'] = 1?

Re: Private Message System help!

Posted: Wed Jan 02, 2013 8:29 pm
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.

Re: Private Message System help!

Posted: Thu Jan 03, 2013 3:05 am
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 ?

Re: Private Message System help!

Posted: Thu Jan 03, 2013 4:50 pm
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?

[syntax=php]$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);[/syntax]

Re: Private Message System help!

Posted: Thu Jan 10, 2013 7:27 pm
by jacek
Is the table conversations_messages I think mine was conversation_messages, either way try sticking a

[syntax=php]echo mysql_error();[/syntax]
after that mysql_query line.

Re: Private Message System help!

Posted: Thu Jan 10, 2013 8:05 pm
by Shahlin
jacek wrote:Is the table conversations_messages I think mine was conversation_messages, either way try sticking a

[syntax=php]echo mysql_error();[/syntax]
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