note 13737 deleted from language.oop by victor
| From: | victor@php.net | Date: | Sat, 07 Dec 2002 15:51:16 +0000 |
| Subject: | note 13737 deleted from language.oop by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40485@lists.php.net to get a copy of this message | ||
Here is a simple example of a class that represents a row in a MySQL database. I have been using
this format for all my work on a large data driven site for some months now and it is _really_
useful.
<?php
class Node {
var $db;
var $id;
var $person;
var $lastChanged;
function Node ($db, $id, $person = false) {
//Creating a software object with no ID means that it is not yet in the database
//Call commit() on it later to save it to database.
$this->db = $db;
$this->id = $id;
if ($id) {
$sql = "SELECT * FROM Node WHERE nodeID=$id";
$res = $db->query("node.inc", $sql);
$dbObj = mysql_fetch_object($res);
if ($person) {
$this->person = $person;
} else {
$this->person = $dbObj->person;
}
$this->lastChanged = $dbObj->lastChanged;
}
else {
$this->person = $person;
}
}
function commit () {
if ($this->id) {
$sql = "UPDATE Node SET " .
"person = $this->person " .
"WHERE nodeID = $this->id";
$res = $this->db->query("node.inc", $sql);
}
else {
$sql = "INSERT INTO Node VALUES " .
"(NULL, $this->person, NOW())";
$res = $this->db->query("node.inc", $sql);
$this->id = mysql_insert_id($this->db->connID);
}
return $this->id;
}
function erase () {
if ($this->id) {
$sql = "DELETE FROM Node WHERE nodeID = $this->id";
$this->db->query("node.inc", $sql);
}
else {
echo "Error: no known node record to delete.";
}
}
}
?>