You can read / manipulate SQL table data by using PHP objects, making writing SQL queries unnecessary in many cases. The following examples show you how to map a PHP class to a database table and how to use these classes to access SQL table data.
Create a class, extend PersistClass and define the name of the SQL table and the name of the primary key column
class TestTable extends PersistClass {
protected $sqlTableName = 'TESTTABLE';
protected $sqlPrimaryKey = 'TESTID';
}
Retrieve the row with primary key = 1
$testRecord = new TestTable(1);
Get data from its columns
$testColumn = $testRecord->getData('TESTCOLUMN');
$otherTestColumn = $testRecord->getData('OTHERTESTCOLUMN');
Modify data in its columns and save changes in the database
$testRecord->setData('testcolumn', 'successfully changed');
$testRecord->setData('othertestcolumn', 'this will change too');
$testRecord->updateDb();
Deleting the record from the Database
$testRecord->deleteDb();
Creating a new record, retrieving its ID (primary key has auto-increment in this example, that's why its not specified)
$testRecord = new TestTable();
$newdata = array('TESTCOLUMN' => 'successfully inserted', 'OTHERTESTCOLUMN' => 'done');
$testRecord = new TestTable($newdata);
$testRecord->insertDb();
$insertedId = $testRecord->getId();
Retrieving all rows
$testRecord = new TestTable(); $allTestRecords = $testRecord->getAllItems();
Retrieving all rows using a where statement
$test = new TestTable();
$tests = $test->getAllItems("where testcolumn='i want rows containing this'");
Retrieving rows with given IDs
$ids = array(1, 2, 3); $test = new TestTable(); $tests = $test->getItemsWithIds($ids);
Note: error handling is described in the error handling section
Check out the next example on