Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:25:47+00:00 2026-06-13T00:25:47+00:00

I am trying to load the people stored in my database into Person objects

  • 0

I am trying to load the people stored in my database into Person objects using the mysqli_result::fetch_object method and then print store them in an array so that I can print the array out as an HTML table

I keep getting a Call to member function on a non-object error on line 84. When I do a var_dump on $row, it prints out NULL. can anyone tell me what I am doing wrong?

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href ="Stylesheet.css" rel="stylesheet" type="text/css">
        <title>Assignment 3</title>
    </head>
    <body>
        <?php
        require ('Person.php');

        //Making an empty array to store Person objects
            $empty = array();


        //Defining parameters for mysqli connect functio

            $p1 = '2011.ispace.ci.edu';
            $p2 = 'username';
            $p3 = 'password';
            $p4 = 'dbname';

        //Connecting to the database and checking to see if it fails        

            $db = new mysqli($p1, $p2, $p3, $p4);
                if ($db->connect_error) 
                {
                    echo "Error Connecting:" . " " . $db->connect_error;
                }

        //Querying the database to get the information from the Person table  

                else 
                {
                $result = $db->query(
                        "SELECT * 
                        FROM Person"
                        );

                        if($result === false){
                            printf($db->error);
                        }

                           var_dump($result);

         //Looping through the rows and storing them in the array

                $totalRows = $result->num_rows;

                if ($totalRows > 0)
                {
                    $emp = 'Person';
                    while($row = $result->fetch_object($emp));
                    {

                        $empty[] = $row;
                        $row++;
                        var_dump($row);

                    }

                }

                }


        //Making the table headers
        echo "<h1>Assignment 3 Incorporated</h1>";
        echo "<table>";

                echo "<tr>";
                echo "<td><strong>Name</strong></td>";
                echo "<td><strong>Title</strong></td>";
                echo "<td><strong>Office</strong></td>";
                echo "<td><strong>Phone Number</strong></td>";
                echo "<td><strong>Email</strong></td>";
                echo "</tr>";

        //Printing out the table with foreach loop

         foreach ($empty as $newPerson) {

                echo "<tr>";

                echo "<td>" . $newPerson-> getfirstName() . " " . $newPerson-> getlastName() . "</td>";
                echo "<td>" . $newPerson-> gettitle() .  "</td>";
                echo "<td>" . $newPerson-> getoffice() .  "</td>";
                echo "<td>" . $newPerson-> getphoneNumber() .  "</td>";
                echo "<td><a href='mailto:" . $newPerson-> getemail() . "'>" . $newPerson-> getemail() .  "</a></td>";

                echo "</tr>";

        }



        echo "</table>"; 

        ?>
    </body>
</html>

Person.php

<?php

class Person {

   private $firstName;
   private $lastName;
   private $title;
   private $office;
   private $phoneNumber;
   private $email;
   //private $data;


 //Constructor

 /*   public function __construct($name)
  {
    $this-> setname($name);
  }
  */ 
//Interface Methods

//Get/Set Name  
  public function setname($name)
  {
    //using explode function to split name into 2 strings
      $this-> name = $name;
      if(is_string($name)) {

           list($lastName, $firstName) = explode(",",$this -> name); 
           $this->firstName = $firstName;
           $this->lastName = $lastName;  

      }

      else {
          user_error('Error: Persons Name Must Be a String!');
      }

  }

  public function getfirstName()
  {

    return $this-> firstName;  

  }


  public function getlastName() 
  {

    return $this-> lastName;   

  }


public function getname()
  {
    /**
    $nameSplit = explode(",",$this ->name);
    foreach ($nameSplit[1] as $firstName);
    foreach ($nameSplit[0] as $lastName);
    echo $firstName . "," . $lastName;
    return $this-> name; 
    **/
  }

  //Get/Set Title
  public function settitle($title)
  {
    $this-> title = $title;  
  }

  public function gettitle()
  {
      return $this-> title;
  }   

  //Get/Set office
  public function setoffice($office)
  {
      $this-> office = $office;
  }

  public function getoffice()
  {
      return $this-> office;
  }

  //Get/Set phone number
  public function setphoneNumber($phoneNumber)
  {
      $this-> phoneNumber = $phoneNumber;  

  }

  public function getphoneNumber()
  {
      //str_replace to modify phone number format
      if(is_string($this-> phoneNumber)) {  
      $find3 = array (')', '(', ' '); 
      $replace3 = array ('', '', '-');
      $phonestr = str_replace($find3, $replace3, $this-> phoneNumber);
      return $phonestr;
      }

      else {
         return user_error("Error: Person's phone number must be a string");
      }
  }

  //Get/Set email
  public function setemail($email)
  {
      $this-> email = $email;
  }

  public function getemail()
  {
      return $this-> email;
  }





      }
  ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:25:49+00:00Added an answer on June 13, 2026 at 12:25 am

    You don’t need multiple loops …

    if ($totalRows > 0) {
        $emp = 'Person';
        while ( $row = $result->fetch_object($emp) )
        {
    
            $newPerson = new person($row);
            echo "<tr>";
    
            echo "<td>" . $newPerson->getfirstName() . " " . $newPerson->getlastName() . "</td>";
            echo "<td>" . $newPerson->gettitle() . "</td>";
            echo "<td>" . $newPerson->getoffice() . "</td>";
            echo "<td>" . $newPerson->getphoneNumber() . "</td>";
            echo "<td><a href='mailto:" . $newPerson->getemail() . "'>" . $newPerson->getemail() . "</a></td>";
    
            echo "</tr>";
        }
    }
    

    You class Constructor should look like this

    class Person {
        private $firstName;
        private $lastName;
        private $title;
        private $office;
        private $phoneNumber;
        private $email;
    
          public function __construct($row) { 
            $this-> setname($row->firtname); 
            $this-> settitle($row->title);
            //And So on ...
          }
      .... So On
    

    Lastly …. Don’t always delete question and posting it back .. always give your question sometime it would surely be answered

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to load content into a div, then on click load the previous content
Trying to load a small .txt file into mysql but get all my data
trying to load in a bunch of images into a list from a directory...my
Im trying to load a image at runtime in WPF using the following code
I'm trying to let people draw a rectangle on Google Maps and store the
I am trying to to load a JSON store with data that I am
I am trying to create an html form that uses AJAX to load database
I'm trying to load a list of objects as grids with several data and
I've been having problems trying to load and play sounds in pygame. Most people
I'm trying to load a flash file using 'AC_FL_RunContent' like below. But the file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.