从表中获取数据并使用OOP显示

时间:2016-01-05 17:13:33

标签: php oop

如何从我的数据库中获取OOP评论?我尝试使用下面的代码,但它显示错误。我是新用的课程,我在这里试图解决这个问题。

以下是评论类中的代码:

function getComments(){
    $komentet = array();
    $connect = mysql_connect("localhost","root","") or die(mysql_error());
    $select = mysql_select_db("profile") or die(mysql_error());
    $id = $_SESSION['id'];
    $result = mysql_query("SELECT * FROM posts order by mycoment DESC") or              die(mysql_error());

    while($row = mysql_fetch_array($result)){
        array_push($komentet,$row['mycoment']);
    }

    return $komentet;
}

使用下面的代码,我试图在我的网页中显示数据,但我不能。

<?php
$komentet = $comm->getComments();

foreach($komentet as $cm){
    echo "<tr><td>".$cm."</td></tr>";
}
?>

它返回以下错误:

  

注意:未定义的变量:第194行的C:\ xampp \ htdocs \ profile \ uprofile.php中的comm       致命错误:在第194行的C:\ xampp \ htdocs \ profile \ uprofile.php中调用null成员函数getComments()

完整课程是以下代码:

 <?php

class Comments {

public $datetime;
public $comment;

 function insertDate(){
    $connect = mysql_connect("localhost","root","") or die(mysql_error());
    $select = mysql_select_db("profile") or die(mysql_error());
    $id = $_SESSION['id'];
    $this->datetime = date("F j, Y, g:i a");
    $sql = "INSERT INTO  posts(userid, mycoment, time) VALUES('$id','$this->comment','$this->datetime')";
    if(mysql_query($sql)){
        echo "Comment Added";
    }else{
        echo "Comment Failed";
    }
 }

 function getComments(){
    $komentet = array();
   $connect = mysql_connect("localhost","root","") or die(mysql_error());
   $select = mysql_select_db("profile") or die(mysql_error());
   $id = $_SESSION['id'];
   $result = mysql_query("SELECT * FROM posts order by mycoment DESC") or die(mysql_error());
   while($row = mysql_fetch_array($result)){
    array_push($komentet,$row['mycoment']);
   }
   return $komentet;
 }

}

1 个答案:

答案 0 :(得分:0)

你遇到的问题是你在一个条件里面写了一个类的实例,它没有通过:

require_once('comments.php');

if(isset($_POST['postsomething'])){ 
   $comm = new Comments();
   $comm->comment = trim($_POST['mycoment']);
   $comm->insertDate();
}

$komentet = $comm->getComments();
foreach($komentet as $cm){
   echo "<tr><td>".$cm."</td></tr>";
}

所以你应该改变它:

require_once('comments.php');

$comm = new Comments();

if(isset($_POST['postsomething'])){ 
   $comm->comment = trim($_POST['mycoment']);
   $comm->insertDate();
}

$komentet = $comm->getComments();
foreach($komentet as $cm){
   echo "<tr><td>".$cm."</td></tr>";
}

我为你做了一些工作,并使用mysqli扩展重构了Comments类,并消除了som DRY(不要重复自己)的漏洞。我强烈建议你坚持使用mysqli并避免任何代码重复。

class Comments
{

   public $datetime;
   public $comment;

   function insertDate(){
      $id = $_SESSION['id'];
      $this->datetime = date("F j, Y, g:i a");
      $sql = "INSERT INTO  posts(userid, mycoment, time) VALUES('$id', '$this->comment', '$this->datetime')";
      if($this->getConnection()->query($sql)){
         echo "Comment Added";
      } else{
         echo "Comment Failed";
      }
   }

   function getComments(){
      $komentet = array();
      $result = $this->getConnection()->query("SELECT * FROM posts order by mycoment DESC");
      while($row = $result->fetch_assoc()){
         array_push($komentet, $row['mycoment']);
      }
      return $komentet;
   }

   private function getConnection(){
      if($this->connection === null)
         $this->connection = mysqli_connect("localhost", "root", "", "profile") or die(mysqli_error($this->connection));
      return $this->connection;
   }

   /** @var mysqli */
   private $connection;
}