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

  • SEARCH
  • Home
  • 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 8472773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:10:08+00:00 2026-06-10T17:10:08+00:00

So I am on a mission to learn how to create DB classes of

  • 0

So I am on a mission to learn how to create DB classes of my own using PDO. I am fairly new to PDO and more complex class development and wanted to get a little guidance from the community before I get too far into it. I have a class partially built but I know there has to be a better/more logical way to do this.

I’d really like to be able to have a single query method so I can trow almost anything at it in my DB class. If this is faulty thinking please let me know why.

Currently I have a config.php file with DB constants defined and a class called DB. Here’s my Code:

In index.php:

    require_once 'config.php';      
    require_once '_inc/class.db.php';
    $db = new DB();

And my class:

        public $dbh;


public function __construct(){

    $dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);

}//end __construct


public function build_query( $args ){

    $defaults = array(
                      'type' => 'SELECT',
                      'fields' => '*',
                      'table' => '',
                      'where' => '',
                      'orderby' => 'id',
                      'order' => 'DESC'
                      'offset' => '0',
                      'limit' => ''
                      );

    $params = array_merge( $defaults, $args );

    $sql = $params['type'];

    $sql .= ' '.$params['fields'].' FROM '.$params['table'];

    if( $params['where'] )
        $sql .= ' WHERE '.$params['where'];

    $sql .= ' ORDER BY '$params['orderby'].' '.$params['order'];

    if( $limit )
        $sql .= ' LIMIT '.$params['offset'].', '.$params['offset'];

    return $sql;    

}//end build_query


public function dbq( $args ){

    $sql = $this->build_query( $args );

    $this->$dbh->prepare( $sql );

    return $this->$dbh->execute();

I know I am missing something here. Please just push me in the right direction as I really want to learn this to become a better PHP developer. I did look a little at using singleton pattern but wanted to get a little more info from people who really know how it all works and fits together.

Thanks in advance!

  • 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. Editorial Team
    Editorial Team
    2026-06-10T17:10:10+00:00Added an answer on June 10, 2026 at 5:10 pm

    If I read your question right, you are looking for a so called Query-Builder. A query builder is a class that has some parameters and which can return the query based on the parameters.

    Actually this needs nothing more and nothing less if the example you have above is what you need. You only need to put that into a class of it’s own because the rest of the database layer has other things to do.

    You can then pass the Query object to where it belongs to. Normally you use that internally in some class. So you are actually not asking about creating a general DB god class (which would be a smell btw), but you just want to wrap the SQL Query String Builder into a class of it’s own:

    /**
     * SQL Query
     */
    class SqlQuery
    {
        public
            $type = 'SELECT',
            $fields = '*',
            $table,
            $where,
            $orderby = 'id',
            $order = 'DESC',
            $offset,
            $limit;
    
        /**
         * @return string
         */
        public function getQuery() {
    
            $sql = sprintf('%s %s FROM %s', $this->type, $this->fields, $this->table);
    
            $this->where
                && $sql .= sprintf(' WHERE %s', $this->where)
            ;
    
            $sql .= sprintf(' ORDER BY %s %s', $this->orderby, $this->order);
    
            $this->limit
                && $sql .= sprintf(' LIMIT %s, %s', $this->offset, $this->limit)
            ;
    
            $sql .= ';'; ### not strictly necessary but nice for debugging ###
    
            return $sql;
        }
    
        public function __toString() {
            return $this->getQuery();
        }
    }
    
    ### Usage: ###
    
    $query = new SqlQuery;
    $query->table = 'TABLE1';
    echo $query; // SELECT * FROM TABLE1 ORDER BY id DESC;
    

    This example actually covers everything you have so far but gives it a defined interface. So you have the default property values and you have the build function. You can then pass that SqlQuery around where you need it.

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

Sidebar

Related Questions

I have two classes, class A and class B. Class A has a mission
So my new mission (lol). I'm looking to create a script that will rotate
My mission is to graduate from using PowerShell to create an instance of Outlook
I am an eighth grader on a mission to create an application in Java
Is it possible to create classes dynamically in Objective-C? I want to do something
We (my team) are about to start development of a mission critical project, one
I have 2 models: class Mission < ActiveRecord::Base belongs_to :category end class Category <
How does one connect to and monitor a remote JVM using JRockit Mission Control
Given a scenario: I have my own system's object structure. Now there are more
I am new to Jrockit mission control so please bear with me. I have

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.