Home arrow PHP arrow Page 4 - Building Object-Oriented Database Interfaces in PHP: Processing Data through Data Access Objects

Inserting and selecting a row: the "submit()" and "load()" methods - PHP

With websites now featuring full-blown dynamic applications that link to databases, data accessing has become a critical process. Often, an object-oriented solution is wanted to manage the data access operations. This works well, except when certain statements are hard-coded in that can cause headaches when a update is required. Alejandro Gervasio explains how a new category of tools, known as database interfaces, help to solve this problem.

  1. Building Object-Oriented Database Interfaces in PHP: Processing Data through Data Access Objects
  2. Overview of a database interface class: defining general guidelines
  3. Source code ahead: listing the code for the "DBIGenerator" class
  4. Inserting and selecting a row: the "submit()" and "load()" methods
  5. Updating and deleting a row: defining the "update()" and "delete()" methods
  6. Factoring a database interface class: the "DBIGenerator" class into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 53
August 10, 2005

print this article



As stated previously, here's the code to generate the "submit()" method:

// create "submit()" method
$str.='function submit(&$db){'."n";
// build insert query
$str.='$sql="INSERT INTO users SET ';
foreach($this->options as $option){
// remove trailing comma
// perform query

As you can appreciate, this method simply packages an "INSERT" clause into the class file and performs the row insertion, by executing the query. Please notice that each method accepts a reference of a MySQL database connection object &$db, for purposes of database connectivity and being able to execute the SQL statement. In this case we're not using the native MySQL PHP functions. Instead, we're utilizing the method "performQuery()" provided by the object $db. Also, the ID of the row inserted is stored, by calling the "getInsertID()" method.

A similar approach is taken with the rest of the methods, thus the "load()" method is generated as listed below:

// create "load" method
$str.='function load(&$db){'."n";
// build query
$str.='$sql="SELECT * FROM users WHERE id='$this->id'";'."n";
// perform query
$str.='return $row;'."n";

In this case, the "load()" method encapsulates a SELECT statement, in order to retrieve an specific row. In this scenario, it's easy to guess how the other methods will be generated. Take a deep breath and join me in the next explanation.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: