user3308065
user3308065

Reputation: 13

PHP var_dump returns reesource(16)

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.

Upvotes: 0

Views: 4962

Answers (3)

Vainglory07
Vainglory07

Reputation: 5293

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.

Upvotes: 0

Krish R
Krish R

Reputation: 22741

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.

Upvotes: 0

xdazz
xdazz

Reputation: 160963

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.

Upvotes: 1

Related Questions