0

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

Mann
  • 3
  • 3
  • your variables will just be available in the scope you defined them. in your case they are available inside the function. you should collect them in an array and return that array from your function – FatFreddy Sep 04 '18 at 08:52
  • oh i see, I am pretty new to php, so do I have to push the variables in array and then return array at the end of function, and then how do I use that at the place I want to print variables? – Mann Sep 04 '18 at 08:56

0 Answers0