Using PHP Objects to Access Your Database Tables (Part 1)
By Tony Marston2005-04-05
Using this Class
The first step is to create a subclass for each physical database table which extends this base class. This must contain its own class constructor specifically tailored to reflect the details of the database table in question. This is done using code similar to the following:
require_once 'default_table.class.inc';Having created a subclass you are then able to include the class definition in any script and create one or more objects from this class. You are then able to start using the class to communicate with your database, as shown in the following code snippets:
class Sample extends Default_Table
{
// additional class variables go here
function Sample ()
{
$this->tablename = 'sample';
$this->dbname = 'foobar';
$this->rows_per_page = 15;
et cetera ...
} // end class constructor
} // end class
include 'sample.class.inc';All data retrieved will now be available as a standard associative array in $data. The following values may also be retrieved if required:
$dbobject = new Sample;
// if $where is null then all rows will be retrieved
$where = "column='value'";
// user may specify a particular page to be displayed
if (isset($_GET['pageno']) {
$dbobject->setPageno($_GET['pageno']);
} // if
$data = $dbobject->getData($where);
$errors = $dbobject->getErrors();
if (!empty($errors)) {
// deal with error message(s)
} // if
$dbobject->numrows will return the total number of rows which satisfied the selection criteria.In the following code snippets $fieldarray may be the $_POST array, or it may be constructed within your PHP script.
$dbobject->pageno will return the current page number based on $rows_per_page.
$dbobject->lastpage will return the last page number based on $rows_per_page.
$fieldarray = $dbobject->insertRecord($fieldarray);
$errors = $dbobject->getErrors();
$fieldarray = $dbobject->updateRecord($fieldarray);
$errors = $dbobject->getErrors();
$fieldarray = $dbobject->deleteRecord($fieldarray);
$errors = $dbobject->getErrors();
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Leave a Comment on "Using PHP Objects to Access Your Database Tables (Part 1)"
You must be logged in to post a comment.
Link to This Tutorial Page!

