0

I have the following code:

function fetch_conversation_messages($conversation_id){
    $conversation_id = (int)$conversation_id;

    $sql = "SELECT
                            `conversations_messages`.`message_date`,
                            `conversations_messages`.`message_date` > `conversations_members`.`conversation_last_view` AS `message_unread`,
                            `conversations_messages`.`message_text`,
                            `users`.`user_name`
                    FROM `conversations_messages`
                    INNER JOIN `users` ON `conversations_messages`.`user_id` = `users`.`user_id`
                    INNER JOIN `conversations_members` ON `conversations_messages`.`conversation_id` = `conversations_members`.`conversation_id`
                    WHERE `conversations_messages`.`conversation_id` = {$conversation_id}
                    AND `conversations_members`.`user_id` = {$_SESSION['user_id']}
                    ORDER BY `conversations_messages`.`message_date` DESC";

    $result = mysql_query($sql);
    var_dump($result);
    $messages = array();

    while (($row = mysql_fetch_assoc($result)) !== false){
            $messages[] = array(
                    'date'          => $row['message_date'],
                    'unread'        => $row['message_unread'],
                    'text'          => $row['message_text'],
                    'user_name'     => $row['user_name'],
            );
    }
    var_dump( $messages );

}

It should return something like this:

Array ( [0] => Array ( [date] => 1322254667 [text] => one [user_name] => bob ) )

However, it returns

resource(16) of type (mysql result) array(0) { }

I am completely new to PHP and MySQL, but I have tried checking for typos, echoing mysql_error, and killing the script at an error, which shows that there is no error in the SQL.

I do not know what has gone wrong and how to fix it.

Please help me. Thanks in advance.

Jenz
  • 8,280
  • 7
  • 44
  • 77
user3308065
  • 13
  • 1
  • 5

3 Answers3

1

var_dump($result); shows resource(16) of type (mysql result) means your query is ok.

var_dump($messages); shows empty array means the result of your query is empty.

xdazz
  • 158,678
  • 38
  • 247
  • 274
0

mysql_query function is expected to return resource when your query is valid.

For you to see the actual returned result of your query, use row fetching functions like mysql_fetch_assoc, mysql_fetch_object, and the like. In your case, you put the fetched values in your $row variable, so you may use var_dump on it.

while ($row = mysql_fetch_assoc($result)){
        var_dump($row);
}

Try to check this reference.

Community
  • 1
  • 1
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
0

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Krish R
  • 22,583
  • 7
  • 50
  • 59