This article is about using and building Database Interfaces (DB interfaces). DB interfaces make application development faster and easier. This is accomplished by relocating scattered DML (Insert, Update, Delete) statements into a single shared tool, the Class. A Class encapsulates data post processing, integrity, and security into a single tool that can be accessed throughout the application.
Here is an example in which a database interface has replaced an embedded DML operation: <?php /* Update the client's contact information with the data from $_POST */ $userID = (int) $_POST['userID']; $email = trim(addslashes($_POST['email'])); $firstname = trim(addslashes($_POST['firstname'])); $lastname = trim(addslashes($_POST['lastname'])); $address1 = trim(addslashes($_POST['address1'])); $address2 = trim(addslashes($_POST['address2'])); $city = trim(addslashes($_POST['city'])); $province = trim(addslashes($_POST['province'])); $country = trim(addslashes($_POST['country' ])); $DML = 'UPDATE clients SET '. "email = '$email',". "firstname = '$firstname',". "lastname = '$lastname',". "address1 = '$address1',". "address2 = '$address2',". "city = '$city',". "province = '$province',". "country = '$country',". "WHERE userID=$userID"; if ($db->Execute($DML)) { // do error handling } ? > <?php $client = new Client(); $client->setUserID ( $_POST['userID' ); $client->setEmail ( $_POST['email' ); $client->setFirstname ( $_POST['firstname'); $client->setLastname ( $_POST['lastname' ); $client->setAddress1 ( $_POST['address1' ); $client->setAddress2 ( $_POST['address2' ); $client->setCity ( $_POST['city' ); $client->setProvince ( $_POST['province' ); $client->setCountry ( $_POST['country' ); if ($client->submit($db) !== true) { // do error handling } ? >
The example above demonstrates a reduction in Application Logic. The DB interface handles all communications with the database, and the programmer can focus on the success or failure of the operation. Using less code to do the same task is most beneficial when there are many parts of the application performing the same DML operations. Rather than duplicating DML queries, you simply use the necessary database interface.