I am currently creating a function that accepts a users id and based on that id it should return all values from the posts database that contains the users id. I have a seperate php file that I saved the function in since I want to use this on many pages. In the functions.php file I have:
class getposts
{
public function getpostcontent($userid){
include('db-conx.php');//Connects to Db
$getval = "SELECT `content`,`date` FROM posts WHERE userid = ?";
$stmt = $conn->stmt_init();
if ($stmt->prepare($getval))
{
$userid = $_SESSION['userid'];
$stmt->bind_param("s", $userid);
$stmt->execute();
$stmt->bind_result($content, $date);
while ($stmt->fetch()) {
$displayname = "Tom";
$array = [
"content" => "$content",
"date" => "$date",
"displayname" => "$displayname",
];
return $array;
}
}
}
and call it using in Posts.php:
$posts = new getposts();
echo $posts ->getpostcontent($userid);
The problem is that the user has multiple rows in the posts database and the code only runs once. How would I go about looping it to show values from each row upon calling it? I may be overthinking it and searched all around but can't seem to get it to work.