Skip to main content

Consolidate query executions

UPDATED:  There is an issue whereby this code does not work as expected with MySQL.  As a result, the concept has been reworked and listed under the entry "Consolidated query executions version 2."


In an effort to adhere to the DRY principle, or don't repeat yourself, the routine defined in this piece can be added to programs that employ a series of queries.  The idea is that it moves a few lines of code that would otherwise need to be in each method or function that calls a query to a database server.

This snippet is coded with PHP and represents a method from a larger class.  There are more details about the code itself in the comments of the code.
private function run($sql)
{
 // Assumes $this->Conn has been set up in advance as a PDO connection.
 $Query = $this->Conn->prepare($sql);
 
 if((func_num_args() == 2) && is_array(func_get_arg(1)))
 {
  $Query->execute(func_get_arg(1));
 }
 else
 {
  $Query->execute(array());
 }
 
 // Assuming a SELECT query.
 try
 {
  // Returns ARRAY
  return $Query->fetchAll(\PDO::FETCH_ASSOC);
 }
 // Otherwise, it's likely an UPDATE, INSERT, or DELETE.
 catch(\PDOException $e)
 {
  // Returns INTEGER
  return $Query->rowCount();
 }
 // Caught some other error.
 catch(\PDOException $e)
 {
  return $e->getMessage();
 }
}
What happens is that the typical lines to actually invoke the query and then collect the results have been moved to this method.  Using this consolidated approach a few lines have been reduced to one.

As a note about this particular implementation, the TRY/CATCH block is an attempt to gracefully handle the results the PHP PDO database interface depending on the type of query that was run.  That's to say, a SELECT instead of an UPDATE, INSERT, or DELETE.  The second catch block will try to catch any actual problems with running the query.

Comments

Popular posts from this blog

PHP built-in webserver and IPv6 addresses

Though it's difficult to confirm around the Web, it appears as though the IP address scheme the built-in PHP server uses depends on the host name that's provided.  If a fully-qualified domain name (FQDN) is used, such as server.domain.tld, then the built-in server will be listening for IPv6 based requests.  If the server is started with an IPv4 address identified as the host, then it will naturally listen for IPv4 based requests. The closest to anything official around the Web was a comment in the PHP docs that only seeded this conclusion, at best.  But here it is in any case. Built-in web server http://php.net/manual/en/features.commandline.webserver.php#120449 Ultimately, the following answer from Stack Overflow held the "universal" solution.  Thank you dew010. Running PHP 5.4 built-in web server outside localhost http://stackoverflow.com/a/8377378/2487485 Start your development server like: php -S 0.0.0.0:8989

Skype Now Supports Emergency Calls In the US

With the latest round of updates to Skype , it was noticed today that the instant messaging and VoIP phone service now supports emergency calling in the United States. The release notes for the most recent update to the Android version of Skype indicated that the service now allows calls to be made to US emergency calling systems via 911.  Telephony services like Skype, Google Voice , and Vonage typically don't offer this emergency calling ability without some sort of intermediate step like deferring to the phone's default dialer, as is the case with Voice, or setting up a physical address location ahead of time which is how Vonage handles it. The indication is that Skype will handle the 911 calls natively, but when verifying with an account that does not have a Skype phone number assigned to it, it was found that the instructions in the FAQ explaining how to turn on emergency calling are not accurate. Emergency calling support in Skype for the United States Interestingly, th...

HP webOS interface kudos

UPDATED 06/07/2013 Pandora no-longer makes the webOS version of its player available. While quite obviously a piece rooted in opinion, a subtle nod is offered to Palm for the excellent interface work it did with webOS .  Since it’s initial release with Sprint in June 2009, Palm quickly slipped into relative oblivion.  With the recent purchase of the company by HP coupled with new product releases set for the summer, webOS may become relevant once again.  If so, then perhaps a whole new lot of users will come to appreciate the consideration that was taken with the webOS user interface (UI). An example is the popular webOS application for the even more popular Pandora Internet radio service.  And as it turns out, the company has a collection of screen captures for the various mobile platforms that have native applications for the service.  This happens to double as a quick way to compare the interface elements of other platforms by using the same applic...