【问题标题】:Accessing classes with MYSQL lookups in PHP在 PHP 中使用 MYSQL 查找访问类
【发布时间】:2011-12-13 08:17:14
【问题描述】:

我正在使用 PHP 制作一个简单的 cmets 系统。对于我的每条评论,我都想查找发表该评论的用户并获取他们的详细信息。

我的课程如下:

class getuser {
    public function __construct($userid) {
        $userquery = "SELECT * FROM users WHERE id = ".$userid."";
        $userresult = mysql_query($userquery);
        while($user = mysql_fetch_array($userresult)){
            $this->uid = $user['id'];
            $this->username = $user['username'];
            $this->avatar = $user['avatar'];
        }
    }
}
class getcomment {
    public function __construct($postid) {
        $commentquery = "SELECT * FROM comments WHERE postid = ".$postid."";
        $commentresult = mysql_query($commentquery);
        while($comment = mysql_fetch_array($commentresult)){
            $this->uid = $comment['uid'];
            $this->comment = $comment['comment'];
            $this->timestamp = $comment['timestamp'];
            $this->likes = $comment['likes'];
            $this->reported = $comment['reported'];

            //get user info for this comment
            $this->getuser = new getuser($this->uid);
        }
    }
}

我在访问我的页面上的这些信息时遇到问题...

                $comments = new getcomment($postid);

                foreach($comments as $comment){

                  echo $comment->username."<br>";

}

数据库中有两个具有此帖子 ID 的 cmets。是导致问题的while循环吗?如何返回所有附有相关用户信息的 cmets?

非常感谢提前

【问题讨论】:

    标签: php mysql oop class


    【解决方案1】:

    while 循环很好,不好的是你的 getcomment 构造函数,
    因为您在循环期间覆盖了每个 $this 属性。

    另外,$comments 是一个对象,而不是一个数组。
    可能的修复:-

    构造函数:-

     $this->comments = array();
     $idx = 0;
     while($comment = mysql_fetch_object($commentresult))
     {
       $this->comments[$idx] = $comment;
       $this->comments[$idx]->getuser = new getuser($comment->uid);
       ++$idx;
    }
    

    访问:-

    $obj = new getcomment($postid);
    foreach($obj->comments as $comment)
    {
      echo $comment->getuser->username."<br>";
    }
    

    【讨论】:

    • 感谢您的回复,我将如何改进我上面写的内容?以及如何循环遍历“对象”?
    • 我只是更新了一个例子,实际上你的问题比我提到的要多。您应该花一些精力阅读一些面向对象设计的示例,这将有助于您更好地理解。
    • 如何在不使用 $comment[1]->getuser->username 的情况下做到这一点?没有数组号,它似乎不起作用...
    • 什么意思?你能var_dump($comment);检查对象(单个对象)吗?
    • array(2) { [0]=> object(stdClass)#3 (9) { ["id"]=> string(3) "113" ["qid"]=> string (2) "13" ["aid"]=> string(1) "0" ["uid"]=> string(1) "1" ["comment"]=> string(31) "3333333333333333333333333333333" ["喜欢"]=> 字符串(1) "0" ["reported"]=> 字符串(1) "0" ["timestamp"]=> 字符串(19) "2011-12-06 10:52:51" [ "getuser"]=> object(getuser)#4 (4) { ["uid"]=> string(1) "1" ["username"]=> string(3) "Tim" ["avatar"]= > string(0) "" ["monster"]=> string(43) "/admin/monsterid/monsters/1300952901924.png" } }
    【解决方案2】:

    如果您创建一个“getUser”函​​数而不是 getuser 属性,也许会更好。或者至少不要将类和属性命名为“getUser”。

    而且你真的不应该让多条记录共享同一个索引。使用唯一索引。还要在 cmets 表的 userid 字段上分配一个外键。

    【讨论】:

      【解决方案3】:

      按照你的做法,你应该回显 $comment->getuser->username。

      您在这里使用的类结构有点奇怪。我建议如下:

      <?php
      
      class Comment {
      
        public $uid, $comment, $timestamp, $likes, $reported;
        protected $user;
      
        public function __construct($result_data)
        {
          foreach ($result_data as $k => $v)
          {
            $this->$k = $v;
          }
        }
      
        public function getUser()
        {
          if (empty($this->user))
          {
            $userquery = "SELECT * FROM users WHERE id = ".$this->uid."";
            $userresult = mysql_query($userquery);
            $this->user = mysql_fetch_object($userresult);
          }
      
          return $this->user;
        }
      
        public static function get_by_post_id($postid)
        {
          $commentquery = "SELECT * FROM comments WHERE postid = ".$postid."";
          $commentresult = mysql_query($commentquery);
          $results = array();
          while($comment_data = mysql_fetch_array($commentresult))
          {
            $results[] = new Comment($comment_data);
          }
          return $results;
        }
      }
      

      然后使用它:

      <?php
      $comments = Comment::get_by_post_id($postid);
      foreach ($comments as $comment)
      {
        echo $comment->getUser()->username;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-05
        • 1970-01-01
        • 2011-02-12
        • 2012-10-23
        • 1970-01-01
        相关资源
        最近更新 更多