Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 165269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:53:28+00:00 2026-05-11T11:53:28+00:00

I built this class to work with PDO, to make SQL queries ‘easier’ and

  • 0

I built this class to work with PDO, to make SQL queries ‘easier’ and less to worry about.

Here are my thoughts

  • Should it be more like class DB extends PDO?
  • Is the query method too big? Should it be split into private methods which are called.. is this what is known as loose coupling?
  • Is my way for detecting a SELECT query too ugly for it’s own good?
  • What other problems are evident? As I am sort of learning-as-I-go, I’m sure I could have overlooked a lot of potential problems.

Thank you

`

 class Db  {     private static $_instance = NULL;       private function __construct() {          // can not call me     }      private function __clone() {          // no!     }      public static function getInstance() {          if (!self::$_instance)         {              try {                  self::$_instance = new PDO('mysql:host=' . CONFIG_MYSQL_SERVER . ';dbname=' . CONFIG_MYSQL_DATABASE, CONFIG_MYSQL_USERNAME, CONFIG_MYSQL_PASSWORD);;                 self::$_instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);              } catch(PDOException $e) {                  trigger_error($e->getMessage());              }          }          return self::$_instance;       }        public static function query($query /*string*/, $bindings = NULL)     {          $queryPortion = substr($query,0, 6);          try {              if ($bindings) {                      $prepared = self::getInstance()->prepare($query);                      foreach($bindings as $binding=>$data) { // defaults to string                          if (!is_array($data)) {                             $prepared->bindParam($binding, $data);                           } else {                              switch(count($data)) {                                  case 1:                                     $prepared->bindParam($binding, $data['value']);                                     break;                                                            case 2:                                     $prepared->bindParam($binding, $data['value'], $data['dataType']);                                     break;                                  case 3:                                     $prepared->bindParam($binding, $data['value'], $data['dataType'], (int)$data['length']);                                     break;                                                            default:                                     trigger_error('An error has occured with the prepared statement bindings.');                                     return false;                                     break;                             }                         }                      }                      $prepared->execute();                      return $prepared->fetchAll(PDO::FETCH_ASSOC);               } else if (String::match($queryPortion, 'select')) { // if this is a select query                  $rows = self::getInstance()->query($query);                  return $rows->fetchAll(PDO::FETCH_ASSOC);              } else {                  return self::getInstance()->exec($query);              }           }          catch(PDOException $e)         {             trigger_error($e->getMessage());         }       }      public static function getLastInsertId()     {         try {             self::getInstance()->lastInsertId();           }         catch(PDOException $e)         {             trigger_error($e->getMessage());         }      }      public static function disconnect()     {         // kill PDO object         self::$_instance = NULL;      }  } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:53:29+00:00Added an answer on May 11, 2026 at 11:53 am

    It’s not bad and as it’s been said it might help for small applications although it’s mostly a very thin abstraction on another abstraction. It’s not bringing a lot of others functionalities.

    Something you might want to consider, amongst other things:

    • As this is PHP5 code, use exceptions instead of trigger_error and set_exception_handler if necessary until exceptions are more widespread, but it’s definitely cleaner and more future-proof.
    • You are using a singleton, it’s not a bad thing necessarily but in this case, for example, one shortcoming will be that you’ll only be able to handle one connection to one database.
    • I don’t know if you make use of stored procedures, but a stored procedure might return a result set through the query() method too.
    • You have two semi-colons (;;) at the end of your new PDO line.

    That being said, I don’t think your query method is too big and there’s not much that could be recalled from elsewhere in there at the moment. Though as soon as you see two or three lines that could be called from another function, split it. That’s a good way to DRY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do that by defining the <=> method for… May 12, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer The problem is that you have a branch called HEAD… May 12, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer Depending on what the innerHTML values of those Dom elements… May 12, 2026 at 8:29 pm

Related Questions

I built this class to work with PDO, to make SQL queries 'easier' and
I miss the .Net remoting days when I could just send an object over
Ok, so everyone has decided (and for good reason) strait SQL is of the
My code is built to multiple .dll files, and I have a template class
I'm preparing a class on Visual Basic 2005 targeting Visual Basic 6 programmers migrating

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.