0

所以基本上,我对 PHP 很陌生,我无法让它工作。我在一个对象中有一个对象,我正在尝试访问它的字段。它从 MySQL 数据库中提取数据。我不确定错误发生在哪里。我希望有人能指出我正在犯的一个公然错误或什么的。谢谢!

<?php
    include("Data/Classes.php");
    include("Data/Utils.php");
    $contacts = listBusinessesWithDefaultContact() or die();
?>
<!DOCTYPE html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Lab 05 - Home</title>
        <link rel="stylesheet" type="text/css" href="Styles/main.css" />
    </head>    
    <body>
        <div id="Wrapper">
            <header>
                <h1>Contacts</h1>
            </header>
            <div id="MainContent">
                <img id="bg" alt="Background" src="Images/Ring.png" />
                <?php

                    foreach ($contacts as $BusinessWithContact) {
                        echo $contacts->Contact->FirstName;
                    }

                ?>
            </div>
            <nav>
                <a id="HomeLink" href="index.php"><img alt="Home Image" src="Images/Home.png" /></a>
                <a id="CreateLink" href="create.php"><img alt="Create Image" src="Images/Create.png" /></a>
                <a id="ReadLink" href="read.php"><img alt="Read Image" src="Images/Read.png" /></a>
                <a id="UpdateLink" href="update.php"><img alt="Update Image" src="Images/Update.png" /></a>
                <a id="DeleteLink" href="delete.php"><img alt="Delete Image" src="Images/Delete.png" /></a>
            </nav>
        </div>
    </body>
</html>

这是“Classes.php”:

<?php
    class Business {
        public $BusinessID;
        public $BusinessName;
        public $BusinessPhone;
        public $BusinessEmail;
        public $Website;
        public $Address;
        public $City;
        public $Province;
        public $PostalCode;
        public $Notes;

        public function __construct($id, $name, $phone, $email, $website, $address, $city, $province, $postalCode, $notes) {
            $this->BusinessID = $id;
            $this->BusinessName = $name;
            $this->BusinessPhone = $phone;
            $this->BusinessEmail = $email;
            $this->Website = $website;
            $this->Address = $address;
            $this->City = $city;
            $this->Province = $province;
            $this->PostalCode = $postalCode;
            $this->Notes = $notes;
        }
    }

    class ContactPerson {
        public $BusinessID;
        public $ContactID;
        public $FirstName;
        public $LastName;
        public $DefaultContact;
        public $Email;
        public $WorkPhone;
        public $CellPhone;
        public $Notes;

        public function __construct ($bid, $cid, $fName, $lName, $dContact, $email, $wPhone, $cPhone, $notes) {
            $this->BusinessID = $bid;
            $this->ContactID = $cid;
            $this->FirstName = $fName;
            $this->LastName = $lName;
            $this->Email = $email;
            $this->WorkPhone = $wPhone;
            $this->CellPhone = $cPhone;
            $this->Notes = $notes;
        }
    }

    class BusinessWithContact {
        public $Business;
        public $Contact;

        public function __construct(Business $business, ContactPerson $contact) {

            $this->Business = $business;
            $this->Contact = $contact;
        }
    }
?>

最后,“Utils.php”:

<?php   
    function listBusinessesWithDefaultContact() {
        $result = array();

        $connection = mysql_connect('[connection]', '[user]', '[pass]') or die (mysql_error());
        mysql_select_db('[db]') or die (mysql_error());

        $dbResult = mysql_query("select * from Business") or die (mysql_error());

        while ($row = mysql_fetch_array($dbResult)) {
            $business = new Business($row['BusinessID'], $row['BusinessName'], $row['BusinessPhone'], $row['BusinessEmail'], $row['Website'], $row['Address'], $row['City'], $row['Province'], $row['PostalCode'], $row['Notes']) or die();

            $queryString = "select * from ContactPerson where BusinessID = " . $row['BusinessID'] . " and DefaultContact = 1";          
            $contactResult = mysql_query($queryString) or die(mysql_error());

            while ($newRow = mysql_fetch_array($contactResult)) {
                $contact = new ContactPerson($newRow['BusinessID'], $newRow['ContactPersonID'], $newRow['FirstName'], $newRow['LastName'], $newRow['DefaultContact'], $newRow['Email'], $newRow['WorkPhone'], $newRow['CellPhone'], $newRow['Notes']) or die();
            }

            array_push($result, new BusinessWithContact($business, $contact) or die());
        }

        return $result;
    }

    function getContact($contactID) {
        $result;

        $connection = mysql_connect('[connection]', '[user]', '[pass]') or die (mysql_error());
        mysql_select_db('[db]') or die (mysql_error());

        $dbResult = mysql_query("select * from Contacts where ContactID = $contactID");

        while ($row = mysql_fetch_array($dbResult)) {
            $result = new Contact($row['ContactID'], $row['Name'], $row['Phone'], $row['Email'], $row['Website'], $row['ContactFirstName'], $row['ContactLastName'], $row['Address'], $row['City'], $row['Province'], $row['Postal']);
        }

        return $result;
    }

    function addContact(Contact $item) {
        $result;

        $connection = mysql_connect('localhost', 'jschaible1', 'dragunov');
        mysql_select_db('jschaible1');

        $result = mysql_query("insert into Contact values($item->Name, $item->Phone, $item->Email, $item->Website, $item->ContactFirstName, $item->ContactLastName, $item->Address, $item->City, $item->Province, $item->Postal)");     

        return $result;
    }

    function updateContact(Contact $item) {
        $result;

        $connection = mysql_connect('[connection]', '[user]', '[pass]') or die (mysql_error());
        mysql_select_db('[db]') or die (mysql_error());

        $result = mysql_query("update Contact values set Name = $item->Name, Phone = $item->Phone, Email = $item->Email, Website = $item->Website, ContactFirstName = $item->ContactFirstName, ContactLastName = $item->ContactLastName, Address = $item->Address, City = $item->City, Province = $item->Province, Postal = $item->Postal");        

        return $result;
    }

    function deleteContact(Contact $item) {
        $result;

        $connection = mysql_connect('[connection]', '[user]', '[pass]') or die (mysql_error());
        mysql_select_db('[db]') or die (mysql_error());

        $result = mysql_query("delete from Contact where ContactID = $item->ContactID");        

        return $result;
    }
?>

编辑:抱歉,澄清一下,我得到的错误是“试图获取非对象的属性”

4

2 回答 2

0

foreach ($contacts as $BusinessWithContact) {
    echo $contacts->Contact->FirstName;
}

您正在使用$contacts->Contact->FirstName而不是$BusinessWithContact->Contact->FirstName. Contact可能不是$contacts.

于 2012-10-17T09:05:49.230 回答
0

显然,如果我改变

 array_push($result, new BusinessWithContact($business, $contact) or die());

在 Utils.php 文件中

$item = new BusinessWithContact($business, $contact);
array_push($result, $item) or die();

有用。我不太确定为什么,这两个应该是相同的,不是吗?

于 2012-10-17T09:11:20.557 回答