I was trying to print a variable inside a method of my class into some other PHP file but I am not able to print that.
I have a class file named Get.php
and I have a function getData()
inside it. The function code is below
public function getData() {
$query = mysqli_query($this->con, "SELECT * FROM blog");
if(!$query) {
echo "<p class='bad'>Unable to fetch blog posts</p>";
} else {
// if query works fine
while($row = mysqli_fetch_array($query)) {
// loops for all the records in the database
$author = $row['author'];
$date = $row['date'];
$title = $row['title'];
$body = $row['body'];
}
}
}
Now I want the variables author, data, title and body to be printed inside my index.php file. To be a bit more clear, I want to do something like this
<h1>THE PHP VARIABLE $title SHOULD BE HERE</h1>
I already made an instace of the class at the top of index.php file by
$get = new Get($connection);
Please help me this and do tell me if any more details on this query is needed.
Thank you for all your helps in advance