简体   繁体   English

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

[英]How can I resolve the “Resource ID #8” error message in MYSQL/PHP?

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";

         }

This returns: Resource id #8 这将返回:资源ID#8

Can you assist me in finding the problem? 您能协助我找到问题吗?

Add this and see what it echo's back 添加此内容,看看它回显了什么

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;

             }

Unless that print_r($data) prints something odd, I don't see what is wrong with that block of code. 除非那个print_r($ data)打印出奇怪的东西,否则我看不出这段代码有什么问题。 May have something to do with the database class you are using. 可能与您使用的数据库类有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM