【问题标题】:function returns boolean instead of appropriate函数返回布尔值而不是适当的
【发布时间】:2014-03-14 07:03:06
【问题描述】:

我正在使用 HTML、PHP、MySQL 和 CSS 创建一个即时消息传递平台。我的函数 fetch_conversation_messages(在 private_message.inc.php 上)。这是private_message.inc.php:

     <?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_message`.`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);
   $messages = array();
   while ($row = mysql_fetch_assoc($result)){
        $messages[] = array(
                'date'          => $row['message_date'],
                'unread'        => $row['message_unread'],
                'text'          => $row['message_text'],
                'user_name'     => $row['user_name'],
        );
  echo mysql_num_rows($result);
           var_dump($row);    

}
return print_r($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);

            }
    }
    ?>

view_conversation.page.inc.php,包含private_message.inc.php的功能如下:

    <?php



    $errors = array();
    $valid_conversation = (isset($_GET['conversation_id']) && validate_conversation_id($_GET['conversation_id']));
    if ($valid_conversation === false){
            $errors[] = 'Invalid Conversation ID.';
    }
    if (isset($_POST['message'])){
            if (empty($_POST['message'])){
                    $errors[] = 'You must enter a message.';
            }

            if (empty($errors)){
                    add_conversation_message($_GET['conversation_id'], $_POST['message']);
            }
    }

 if (empty($errors) === false){
        foreach ($errors as $error){
            echo $error;
        }
}


    if ($valid_conversation){
            /*if (isset($_POST['message'])){
                    update_conversation_last_view($_GET['conversation_id']);*/
                    $messages = fetch_conversation_messages($_GET['conversation_id']);
                    print_r($messages);
            }else{
                    $messages = array();
                    update_conversation_last_view($_GET['conversation_id']);
            }


    ?>
                <p class="name">Username: <?php echo $message['user_name']; ?></p>
            <p class="text">Date: <?php echo date('d/m/Y H:i:s', $message['date']); ?></p>
            <p>Message: <?php echo $message['text']; ?></p>
    <a href="index.php?page=inbox">Inbox</a>
    <a href="index.php?page=logout">Logout</a>

            <form action="" method="post">
                    <p><textarea name="message" rows="10" cols="110"></textarea></p>
                    <p><input type="submit" value="Add Message" /></p>
            </form>

            <?php
            var_dump( $messages );
            if($messages){
            foreach ($messages as $message){
            ?>
            <?php if ($message['unread']) echo 'unread'; ?>


    <?php

    }}
    ?>

我得到以下输出:

数组 ( ) 1 bool(true) 警告:在第 52 行的 /home/u406538221/public_html/public_html/gamma/core/pages/view_conversation.page.inc.php 中为 foreach() 提供的参数无效

我对这个论坛、数据库、PHP 和 MySQL 完全陌生。

问题:无效参数 返回布尔值 - 为什么?我该如何解决?

请帮帮我。

提前谢谢你。

【问题讨论】:

  • 为什么要定义两次 fetch_conversation_messages 函数?
  • 我改了但遇到了同样的问题
  • 你能用你拥有的最新代码更新上面的代码吗?这将有助于找出问题
  • foreach ($messages as $message){ 这是您遇到错误的行吗?或者这个 foreach ($errors as $error){
  • foreach ($messages as $message)

标签: php html mysql sql boolean


【解决方案1】:

首先你的fetch_conversation_messages 函数返回print_r($messages)。这样做是打印$messages 变量(这是Array() 的来源),然后它返回true。然后在调用fetch_conversation_messages 之后,您正在执行print_r($messages) 并且由于$messagesbooleanprint_r 正在打印1。然后你的 var_dump($messages) 在第 52 行的 foreach 之前,$messages 变量是一个 boolean 等于 true,这就是 bool(true) 输出的来源。因此,您正试图循环遍历 boolean 之类的数组,这会为您提供有关为 foreach() 提供的无效参数的错误。所以你需要做的只是在你的fetch_conversation_messages函数中返回$messages而不是print_r($messages)

编辑:

这就是您的fetch_conversation_messages 的样子:

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_message`.`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);
    $messages= array();
    while ($row = mysql_fetch_assoc($result)){
        $messages[] = array(
            'date'          => $row['message_date'],
            'unread'        => $row['message_unread'],
            'text'          => $row['message_text'],
            'user_name'     => $row['user_name'],
    );

    echo mysql_num_rows($result);
    var_dump($row);    

    }
    return $messages;
}

【讨论】:

  • 我已经按照你说的做了,但是我仍然得到 bool(true)
  • 更改后的输出是什么?还是输出Array()
  • Array () 1 用户名:日期:31/12/1969 19:00:00 消息:收件箱注销 bool(true) 警告:/home/u406538221/public_html 中为 foreach() 提供的参数无效/public_html/gamma/core/pages/view_conversation.page.inc.php 第 52 行
  • 我编辑了我的答案,上面是你的fetch_conversation_messages函数应该有的内容。
  • 当我这样做时,我得到这个:警告:mysql_fetch_assoc() 期望参数 1 是资源,在 /home/u406538221/public_html/public_html/gamma/core/inc/private_message.inc 中给出的布尔值第 52 行 .php 数组 () 用户名​​:日期:31/12/1969 19:00:00 消息:收件箱注销数组(0){}
猜你喜欢
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多