userId = $row['userId']; $this->firstName = $row['firstName']; $this->lastName = $row['lastName']; $this->email = $row['email'] } public function insert() { $sql = "INSERT INTO User SET firstName='" . mysql_escape_string($this->firstName) . "', lastName='" . mysql_escape_string($this->lastName) . "', email='" . mysql_escape_string($this->email) . "'"; mysql_query($sql); $this->userId = mysql_insert_id(); } public function update() { $sql = "UPDATE User SET firstName='" . mysql_escape_string($this->firstName) . "', lastName='" . mysql_escape_string($this->lastName) . "', email='" . mysql_escape_string($this->email) . "' WHERE userId=" . mysql_escape_string($this->userId); mysql_query($sql); } public function delete() { $sql = "DELETE FROM User WHERE userId=" . mysql_escape_string($this->userId); mysql_query($sql); } public function find() { $sql = "SELECT * FROM User"; // This array will hold the where clause $where = array(); // Using PHP 5's handy new reflection API $class = new ReflectionClass('DO_User'); // Get all of DO_User's vairable (or property) names $properties = $class->getProperties(); // Loop through the properties for ($i = 0; $i < count($properties); $i++){ $name = $properties[$i]->getName(); if ($this->$name != ''){ // Add this to the where clause $where[] = "`" . $name . "`='" . mysql_escape_string($this->$name) . "'"; } } // If we have a where clause, build it if (count($where) > 0){ $sql .= " WHERE " . implode(' AND ', $where); } $rs = mysql_query($sql); include_once('class-ReadOnlyResultSet.php'); return new ReadOnlyResultSet($rs); } } ?>