In the previous section, I defined and implemented the constructor of the MySQL abstraction class, a process that should be pretty understandable to you. Now, it’s time to extend the class’s current functionality by adding to it a brand new chainable method for creating the SELECT portion of a query. The implementation of this method, called “select(),” is show below. Take a look at it: // create the SELECT part of the query // chainable public function select($table, $fields = '*') { $this->query = ' SELECT ' . $fields . ' FROM ' . $table; return $this; } See how simple it is to define a method that builds the SELECT part of a SQL query? I guess you do. In this specific case, the method not only will create that portion of the query according to the specified fields, but since it returns an instance of the MySQL class, it can be linked easily to others as well. But I’m getting ahead of myself; please pay attention to the following code sample, which shows the signature of the sample “MySQL” class, this time including the “select()” method: class MySQL { private $host = ''; private $user = ''; private $password = ''; private $database = ''; private $query = ''; private $result = NULL; private $link = NULL; private static $instance = NULL;
// constructor public function __construct($host, $user, $password, $database) { if (FALSE === ($this->link = mysqli_connect($host, $user, $password, $database))) { throw new Exception('Error : ' . mysqli_connect_error()); } }
// create the SELECT part of the query // chainable public function select($table, $fields = '*') { $this->query = ' SELECT ' . $fields . ' FROM ' . $table; return $this; } } The above “MySQL” class is beginning to take shape, even though in its current state it is still quite basic. However, this is about to change; in the next section I’m going to add to it yet another chainable method, which will be responsible for building the WHERE modifier of a SQL query. To learn how this will be accomplished, please click on the link below and read the next few lines.
blog comments powered by Disqus |
|
|
|
|
|
|
|