K.Wayne K.Wayne - 4 years ago 137
PHP Question

Get datas from table and display with OOP

How I can get the comments with OOP from my database? Im trying with below code but it displaying the error. Im new using the classes and I git stuck here trying to reselve this issue.

Below is the code in class Comments:

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


With the below code, Im trying to display the datas in my web page but I can not.

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

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


It returning the following error:


Notice: Undefined variable: comm in C:\xampp\htdocs\profile\uprofile.php on line 194
Fatal error: Call to a member function getComments() on null in C:\xampp\htdocs\profile\uprofile.php on line 194


Full classs is the code below:

<?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;
}

}

Answer Source

Does this work if you put it in the file where you have the echo part now? If not, add the second relevant php code in your question. If so, divide it into two files and test it out ;)

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

$comm = new Comments();
$komentet = $comm->getComments();
foreach($komentet as $cm){
   echo "<tr><td>".$cm."</td></tr>";
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download