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.

4
  • Seems like your query failed ? Commented Feb 14, 2014 at 6:25
  • I thought that but I cannot see how; do you have any ideas? Commented Feb 14, 2014 at 6:26
  • try with echo count($messages); if print 0 no result is find in your query Commented Feb 14, 2014 at 6:27
  • I did that, but all I got was resource(16) of type (mysql result) 0 Commented Feb 14, 2014 at 6:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Do you have any ideas for how to fix it. Would sending you the SQL file be a goood idea, as I am a complete noob in MySQL and PHP
@user3308065 print your sql and try run it in mysql client, fix the sql is your job :).
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SELECT conversations_messages.message_date' at line 1 comes out of PhpMyAdmin
then you have a problem in the query
OK, but (n00b question alert) how do I fix it?
|
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.