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