0

I developed an object-oriented guestbook and I have my Class GuestBookEntry:

class GuestBookEntry {
protected $id;
protected $entryDate;
protected $authorName;
protected $authorMail;  
protected $entryText; 

// getters, setters and specific functions are omitted
}

I also have a mysql table with the same names as columns.

Well, when I fetch guestbook entries, it seems to work, but it only displays the ID and the Date, which where returned by getters. The other things won't return to the template.

The code looks like this:

public function showEntries() { 
    $query = mysql_query('SELECT * FROM dng_entries ORDER BY id DESC');     
    while($entry = @mysql_fetch_object($query, 'GuestBookEntry')) {
        if(empty($entry)) {
        echo '<font color="white">Keine Eintr&auml;ge vorhanden...</font>';
        } else {    
        var_dump($entry);
        echo '<table class="table table-bordered entryfield" align="right">
                <thead>
                    <tr>
                        <td rowspan="2">'.$entry->getId().'</td>
                        <td width="20%">Datum:</td>
                        <td>Name:</td>
                        <td>Email:</td>
                    </tr>
                    <tr>
                        <td>'.$entry->getEntryDate().'</td>
                        <td>'.$entry->getAuthorName().'</td>
                        <td><a href="mailto:'.$entry->getAuthorMail().'">'.$entry->getAuthorMail().'</a></td>
                </thead>
                <tbody>
                    <tr>
                    <td width="10%" valign="middle">Eintrag:</td>
                    <th colspan="3" valign="top" height="100px">'.$entry->getEntryText().'</td>
                    </tr>
                </tbody>
            </table>';
        }
    }
}

Here's a var_dump of e.g. object:

object(GuestBookEntry)#2 (5) { ["id":protected]=> string(1) "2" ["entryDate":protected]=> int(1344696811) ["authorName":protected]=> NULL ["authorMail":protected]=> NULL ["entryText":protected]=> NULL }

update: well here is the rest of GuestBookEntry class:

public function __construct($authorName, $authorMail, $entryText) {
       $this->authorName    = $authorName;
       $this->authorMail    = $authorMail;
       $this->entryDate     = time();
       $this->entryText     = $entryText;
    }

    public function getId() {
       return $this->id;
    }
    public function getAuthorName() {
       return (String) $this->authorName;
    }
    public function getAuthorMail() {
       return (String) $this->authorMail;
    }
    public function getEntryDate() {
       return date('d.n.Y', $this->entryDate);
    }
    public function getEntryText() {
       return $this->entryText;
    }

    public function setAuthorName($authorName) {
       $this->authorName=$authorName;
    }   
    public function setAuthorMail($authorMail) {
       $this->authorMail=$authorMail;
    }
    public function setEntryDate($entryDate) {
       $this->entryDate=$entryDate;
    }
    public function setEntryText($entryText) {
       $this->entryText=$entryText;
    }
4
  • 4
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. And don't use the @ operator. There might be something funny going on in your guestBookEntry class getters/setters, show their code. Commented Aug 11, 2012 at 14:57
  • The var_dump() output you include shows that some of your attributes, e.g. authorName are NULL. Have you checked the data the query is returning? Commented Aug 11, 2012 at 14:59
  • What the heck is mysql_fetch_object. Don't use it. Commented Aug 11, 2012 at 15:00
  • Please add your db create statement. Commented Aug 11, 2012 at 15:05

1 Answer 1

2

Your problem is case sensitivity. MySQL column names don't deal with camel case, but PHP see's a difference between properties based on case such as $entryDate and $entrydate. Stick to lowercase with underscores if you need visual seperation. The reason why id works is it's all lower case.

I think if you simply lowercase all your property names that are supposed map to table columns everything will work.

BTW... The camel case issue with column names may vary based on the OS running the sql server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.