0

我正在尝试使用 mysqli_result::fetch_object 方法将存储在我的数据库中的人员加载到 Person 对象中,然后将它们打印存储在一个数组中,以便我可以将数组打印为 HTML 表

我在第 84 行的非对象错误上不断收到对成员函数的调用。当我在 $row 上执行 var_dump 时,它会打印出 NULL。谁能告诉我我做错了什么?

索引.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>

人.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;
  }





      }
  ?>
4

1 回答 1

0

你不需要多个循环......

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>";
    }
}

你的类 Constructor 应该是这样的

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

最后..不要总是删除问题并将其发回..总是在某个时候提出您的问题,它肯定会得到回答

于 2012-10-15T00:55:14.003 回答