1

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

}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
K.Wayne
  • 87
  • 10
  • Where has `$comm` been initialized? – Script47 Jan 05 '16 at 17:17
  • You need to initialize $comm. $comm = new Comments(); – pajamas Jan 05 '16 at 17:23
  • @Fred-ii- I think `getComments` is in the class `Comments`. – Script47 Jan 05 '16 at 17:26
  • @K.Wayne Do not make any $comm global, please - that's not a correct OOP aproach. And do not use mysql extension, use mysqli instead. Usage is same, but it's not deprecated and works faster and safer. Try to help us helping you and edit your question, put there all (relevant) code you are working with. – Reloecc Jan 05 '16 at 17:30
  • there are too many comments here and no solution yet. Post your full code. all these comments should then be deleted once OP posts full code. – Funk Forty Niner Jan 05 '16 at 17:31
  • The code above is in my profile.php script where Im trying to Echo the results – K.Wayne Jan 05 '16 at 17:35
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Jan 05 '16 at 17:44
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 05 '16 at 17:45
  • you edited but still haven't included where you assign/initialized `$comm`. Closest I see is `public $comment;`. Is that what you meant to use here? and what's line 194? – Funk Forty Niner Jan 05 '16 at 17:46
  • @Fred-ii- The $comm I habe initialize in other script where I want to display the result. It is initialised as below: require_once('comments.php');if(isset($_POST['postsomething'])){ $comm = new Comments(); $comm->comment = trim($_POST['mycoment']); $comm->insertDate(); } Also in line 194 is 'foreach' – K.Wayne Jan 05 '16 at 17:48
  • well if a conditional statement is failing or an include/require, I couldn't say. I'll have to pass on the question, good luck. I sincerely hope you find your solution, *cheers* – Funk Forty Niner Jan 05 '16 at 17:49

1 Answers1

0

the problem you have is you crate the instance of the class Comments inside of a condition, which does not pass:

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

so you should alter it like this:

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

I've made a little work for you and refactored the Comments class using mysqli extension and ridding som DRY (Don't Repeat Yourself) breaches. I highly suggest you to stick on mysqli and avoid any code duplication.

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;
}
Reloecc
  • 256
  • 2
  • 13
  • Damn, I have tried so much and this is the correct answer. Thanks a lot @Reloecc :) :), thanks ans to others for the effort. – K.Wayne Jan 05 '16 at 17:57
  • so it was a non-include then, just as I mentioned in [this comment](http://stackoverflow.com/questions/34617440/get-datas-from-table-and-display-with-oop#comment56981640_34617440) to the OP. I kind of had a feeling it was variable scope issue. – Funk Forty Niner Jan 05 '16 at 17:58
  • I've edited the answer so it's clear for everyone not deeply interested in. Glad to help. – Reloecc Jan 05 '16 at 18:34