2012-10-15 32 views
0

我試圖使用mysqli_result :: fetch_object方法將存儲在我的數據庫中的人加載到Person對象中,然後將它們存儲在數組中,以便我可以將數組作爲HTML表格使用mysqli方法將人物對象加載到數組中

我不斷收到對第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; 
    } 





     } 
    ?> 

回答

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 

最後....不要總是刪除的問題和張貼回來..總是給你的問題的某個時候它肯定會回答

相關問題