使用mysqli方法将人物对象加载到数组中

时间:2012-10-15 00:49:07

标签: php mysqli

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

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

的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 个答案:

答案 0 :(得分: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>";
    }
}

您的类构造函数应该如下所示

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

最后....不要总是删除问题并将其发回去。总是在某些时候提出问题肯定会得到回答