HomePHP 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.
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:
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.