2014-02-13 55 views
-1

我遇到了PHP/MySQL即時消息系統的函數問題。一個變量($ sql)被定義爲一個MySQL查詢,然後查詢它並返回一個資源ID。但是,下面的while語句返回'Array'而不是其假定的內容。PHP MySQL數組錯誤

<?php 

// Fetches a summary of the conversations. 
function fetch_conversation_summary(){ 
     $sql = "SELECT 
           `conversations`.`conversation_id`, 
           `conversations`.`conversation_subject`, 
           MAX(`conversations_messages`.`message_date`) AS `conversation_last_reply`, 
           MAX(`conversations_messages`.`message_date`) > `conversations_members`.`conversation_last_view` AS `conversation_unread` 
         FROM `conversations` 
         LEFT JOIN `conversations_messages` ON `conversations`.`conversation_id` = `conversations_messages`.`conversation_id` 
         INNER JOIN `conversations_members` ON `conversations`.`conversation_id` = `conversations_messages`.`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'], 
         'unread_messages'  => ($row['conversation_unread'] == 1), 
       ); 
     } 

     return $conversations; 
} 

// Fetches all of the messages in the given converstion. 
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); 
    echo $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); 
     } 


// Sets the last view time to the current time for the given conversation. 
function update_conversation_last_view($conversation_id){ 
     $conversation_id = (int)$conversation_id; 
     $time = time() + 18000; 
     $sql ="UPDATE `conversations_members` 
         SET `conversation_last_view` = {$time} 
         WHERE `conversation_id` = {$conversation_id} 
         AND `user_id` = {$_SESSION['user_id']}"; 

     mysql_query($sql);  
} 

// Creates a new conversation, making the given users a member. 
function create_conversation($user_ids, $subject, $body){ 
     $subject  = mysql_real_escape_string(htmlentities($subject)); 
     $body   = mysql_real_escape_string(htmlentities($body)); 

     mysql_query("INSERT INTO `conversations` (`conversation_subject`) VALUES ('{$subject}')"); 

     $conversation_id = mysql_insert_id(); 

     $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); 

     $values = array("({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), 0)"); 

     foreach ($user_ids as $user_id){ 
       $user_id = (int)$user_id; 

    $values = array("({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), 0)");   } 

     $sql = "INSERT INTO `conversations_members` (`conversation_id`, `user_id`, `conversation_last_view`, `conversation_deleted`) 
         VALUES " . implode(", ", $values); 

         mysql_query($sql); 
} 


// Checks to see if the given user is a member of the given conversation. 
    function validate_conversation_id($conversation_id){ 
    $conversation_id = (int)$conversation_id; 

    $sql = "SELECT COUNT(1) 
    FROM `conversations_members` 
    WHERE `conversation_id` = {$conversation_id} 
    AND `user_id` = {$_SESSION['user_id']} 
    AND `conversation_deleted` = 0"; 

    $result = mysql_query($sql); 
    return(mysql_result($result, 0) == 1); 
    } 
    // Adds a message to the given conversation. 
    function add_conversation_message($conversation_id, $text){ 
      $conversation_id  = (int)$conversation_id; 
       $text     = mysql_real_escape_string(htmlentities($text)); 

     $sql = "INSERT INTO `conversations_messages` (`conversation_id`, `user_id`, `message_date`, `message_text`) 
         VALUES ({$conversation_id}, {$_SESSION['user_id']}, UNIX_TIMESTAMP(), '{$text}')"; 

     mysql_query($sql); 

     mysql_query("UPDATE `conversations_members` SET `conversation_deleted` = 0 where `conversation_id = {$conversation_id}"); 
} 

// Deletes (or marks as deleted) a given conversation. 
function delete_conversation($conversation_id){ 
     $conversation_id = (int)$conversation_id; 

     $sql = "SELECT DISTINCT `conversation_deleted` 
         FROM `conversations_members` 
         WHERE `user_id` != {$_SESSION['user_id']} 
         AND `conversation_id` = {$conversation_id}"; 

     $result = mysql_query($sql); 

     //if (mysql_num_rows($result) == 1 && mysql_result($result, 0) == 1){ 
     if (mysql_num_rows($result) == 0){ 
       mysql_query("DELETE FROM `conversations` WHERE `conversation_id` = {$conversation_id}"); 
       mysql_query("DELETE FROM `conversations_members` WHERE `conversation_id` = {$conversation_id}"); 
       mysql_query("DELETE FROM `conversations_messages` WHERE `conversation_id` = {$conversation_id}"); 
     }else{ 
       $sql = "UPDATE `conversations_members` 
           SET `conversation_deleted` = 1 
           WHERE `conversation_id` = {$conversation_id} 
           AND `user_id` = {$_SESSION['user_id']}"; 

       mysql_query($sql); 

     } 
} 
?> 

我試過回顯錯誤消息(正如您從代碼中看到的那樣)。另外,我還回應了SQL查詢的結果。

更新:我沒有提到的var_dump,我的結果是: 資源ID#16array(0){}()數組

+0

你看到了什麼錯誤信息? –

+0

沒有錯誤消息,但是'echo $ messages'只是給了我'數組',當我回顯$ result時,我得到'resource id#16' – user3308065

+0

@ user3308065這是絕對正常的。 – kapa

回答

4

當然它打印陣列,因爲這是$消息的。如果您想打印$消息的內容,您可以使用var_dumpprint_r

$ result的原因相似。 $ result是一種資源類型(至少只要查詢成功)。打印它是沒有意義的,因爲它是內部參考。您必須將其傳遞給像mysql_fetch_assocmysql_fetch_array或類似的可以處理此類資源的功能。


關於您更新的問題:此輸出提示您的數組爲空且不包含任何元素。你確定你的SQL查詢返回任何行嗎?

+0

請給我一個例子 – user3308065

+0

@ user3308065他給了你兩個鏈接,我向你保證有足夠的例子。 – kapa

+0

對不起 - 當涉及到PHP和MySQL時,我是一個大白菜 – user3308065