如何在MYSQL / PHP中解决“资源ID#8”错误消息?

时间:2011-08-08 14:20:40

标签: php mysql

function show_post($user_id)
             {
                 global $database;
                 $post = array();

                 $sql = sprintf("SELECT body,stamp FROM post WHERE user_id = '%s' ORDER BY stamp DESC ",$user_id);

                 $result = $database->query($sql);

                 while( $data = $database -> fetch_array($result))
                 {
                        $post[] = array(
                         'user_id' => $user_id,
                         'body' => $data->'body',
                         'stamp' => $data->'stamp'
                         );


                 }

                   return $post;

             }

$posts = $link->show_post($_SESSION['user_id']);

         if(count($posts))
         {
        ?>
        <table border='1' cellspacing='0' cellpadding='5' width='500'>
        <?php
             foreach($posts as $key => $list)
             {
                echo "<tr valign='top'>\n";
                echo "<td>".$list['user_id'] ."</td>\n";
                echo "<td>".$list['body'] ."<br/>\n";
                echo "<small>".$list['stamp'] ."</small></td>\n";
                echo "</tr>\n";

             }


         ?>
            </table>
         <?php
         }else
         {
             echo "nothing entered";

         }

返回:资源ID#8

你能协助我找到问题吗?

1 个答案:

答案 0 :(得分:0)

添加此内容并查看它回显的内容

function show_post($user_id)
             {
                 global $database;
                 $post = array();

                 $sql = sprintf("SELECT body,stamp FROM post WHERE user_id = '%s' ORDER BY stamp DESC ",$user_id);

                 $result = $database->query($sql);

                 while( $data = $database -> fetch_array($result))
                 {
                        echo '<pre>'; print_r($data); echo '</pre><br />';

                        $post[] = array(
                         'user_id' => $user_id,
                         'body' => $data->'body',
                         'stamp' => $data->'stamp'
                         );


                 }

                   return $post;

             }

除非print_r($ data)打印出奇怪的东西,否则我看不出那段代码有什么问题。可能与您正在使用的数据库类有关。