Hello everyone,
I am new to PHP and learning it. I am trying to fetch data from the database and want to store the comments in an empty array called $comments[ ]. I am trying to do this in a function called get_comments( ). And then I have to output the comments on the html page by coding in different php file called template_function.php where there is one more function called the_comments( ) in it. I am using a while loop to fetch all rows using fetchAll( ) method in the database.php file and the index.php fie has the array declaration in it but unfortunately the data is being extracted through the print_r statement not the echo statement.
Thanks in advance

Here are the php file codings:
index.php:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// global array of posts, to be fetched from database
$comments = [];
// global array of unique commenter email addresses to be fetched from db
$commenters = [];

require_once 'database/database.php';
require_once 'templates/functions/template_functions.php';

//connect to database: PHP Data object representing Database connection
$pdo = db_connect();
// submit comment to database
handle_form_submission();

// Get comments from database
get_comments();
// Get commenters from database
get_commenters();

// include the template to display the page
include 'templates/index.php';

database.php file:
<?php
require_once('config.php');
// Should return a PDO
function db_connect() {


try {
// TODO
// try to open database connection using constants set in config.php
// return $pdo;
$servername = DBHOST;
$databasename = DBNAME;
$user = DBUSER;
$password = DBPASS;

$pdo = new PDO("mysql:host=$servername;dbname=$databasename",$user,$password);
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

return $pdo;

}
catch (PDOException $e)
{
die($e->getMessage());
}
}

// Handle form submission
function handle_form_submission() {
global $pdo;

if($SERVER["REQUEST_METHOD"] == "POST")
{
// TODO
// Prepare the submitted form data and insert it to the database
$email = $
POST['email'];
$mood = $POST['mood'];
$comments = $
POST['comment'];
$date = date('Y-m-d');

$stmt = $pdo->prepare("INSERT INTO comments (email,mood,commentText,date) VALUES(:email,:mood,:commentText,:date)");
$stmt->bindParam(':email',$email);
$stmt->bindParam(':mood',$mood);
$stmt->bindParam(':commentText',$comments);
$stmt->bindParam(':date',$date);
$stmt->execute();

}
}

// Get all comments from database and store in $comments
function get_comments() {
global $pdo;
global $comments;

//TODO
$sql = "SELECT * FROM comments ORDER BY ID DESC";
$result = $pdo->query($sql);

while($rows = $result->fetchAll()){
$comments = $rows;
//echo "<pre>";
//print_r($comments);
//echo $comments."<br>";
}


}

// Get unique email addresses and store in $commenters
function get_commenters() {
global $pdo;
global $commenters;

//TODO

}

template_function.php file:
<?php
require_once('database/database.php');
// Output comments to HTML
function the_comments() {
global $comments;

// TODO
get_comments();
//echo "<div class = 'comments'>"."<h2>".'Comments'."</h2>"
//."<div class='comment'>".$comments."</div>"."</div>";

foreach($comments as $values){
print_r($values);
}
}

// Output unique email addresses to HTML
function the_commenters() {
global $filter;
global $commenters;

//TODO

}

    Write a Reply...