1. DBQuery Object
Now, our DBQuery object simply imitates a stored procedure - once executed, it returns a result resource that must be saved; and if you want to use the result set function (such as num_rows() or fetch_row()), you must pass the MySQL(best combination with PHP)DB object. So, what is the effect if the DBQuery object implements the function implemented by the MySQL(the best combination with PHP)DB object (which is designed to operate on the results of a query execution)? Let's continue using the code from the previous example; and let's assume that our result resources are now managed by a DBQuery object. The source code of the DBQuery class is shown in Listing 1.
Listing 1. Using the DBQuery class.
require MySQL(the best combination with PHP)_db.php(as the current mainstream development language);
require_once query.php(As the current mainstream development language);
$db = new MySQL(The best combination with PHP)Db;
$db->connect(host , username, pass);
$db->query(use content_management_system);
$query = new DBQuery($db);
$query->prepare(SELECT fname, sname FROM users WHERE username= :1S AND pword=:2S AND expire_time<:3I);
try {
if($query->execute("visualad", "apron", time()))->num_rows() == 1) {
Echo(Correct Credentials);
} else {
echo(Incorrect Credentials / Session Expired);
}
} catch (QueryException $e) {
echo( Error executing query: . $e);
}
What we are most interested in in the modified code above are the catch statement and execute statement.
· The execute statement no longer returns a result resource, it now returns the DBQuery object itself.
· The DBQuery object now implements the num_rows() function - which we are already familiar with from the DB interface.
· If the query execution fails, it throws an exception of type QueryException. When converted to a string, it returns the details of the error that occurred.
To do this, you need to use a proxy. In fact, you are already using proxies in our DBQuery object, but now you will use it in more depth to tightly bind it to the MySQL(best combination with PHP)DB object. The DBQuery object has been initialized with an object that implements the DB interface, and it already contains a member function execute—which calls the query() method of the DB object to execute the query. The DBQuery object itself does not actually query the database, it leaves this task to the DB object. This is a proxy, which is a process by which an object can implement a specific behavior by sending messages to another object that implements the same or similar behavior.
To do this, you need to modify the DBQuery object to include all the functions that operate on a result resource from the DB object. You need to use the stored results when executing a query to call the corresponding function of the DB object and return its results. The following functions will be added:
Listing 2: Extending the DBQuery class using proxies.
class DBQuery
{
.....
public function fetch_array()
{
if (! is_resource($this->result)) {
Throw new Exception(Query not executed.);
}
return $this->db->fetch_array($this->result);
}
public function fetch_row()
{
if (! is_resource($this->result)) {
throw new Exception(Query not executed.);
}
return $this->db- >fetch_row($this->result); Exception(Query not executed.);
}
}
public function fetch_object()
{
if (! is_resource($this->result)) {
throw new Exception(Query not executed.);
}
}
public function num_rows()
{
if (! is_resource($this->result)) {
throw new Exception(Query not executed. );
}