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 8273753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:33:50+00:00 2026-06-08T07:33:50+00:00

I have a database class that uses PDO. Here’s a portion example of it:

  • 0

I have a database class that uses PDO. Here’s a portion example of it:

class db {
private $host;
private $username;
private $password;
private $con;
    private $pdo;

public function __construct( $database = "dnname" )
{
    $this->host = "localhost";
    $this->username = "username";
    $this->password = "pass";
            $conStr = "host={$this->host};dbname={$database}";
            try {
                $this->pdo = new PDO( "mysql:$conStr", $this->username, $this->password );
                $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
            }
            catch( PDOException $e ) {
                echo "error ". $e->getMessage();
            }
}

public function fetch_single_row($sql, $data)
{
    if( $data !== null )
        $data = array_values( $data ); // Incase it's an associative array
    $sel = $this->pdo->prepare( $sql );
    $sel->execute( $data );
    $sel->setFetchMode( PDO::FETCH_OBJ );
    $obj = $sel->fetch();
    return $obj;
}

I would like to use this class and its functions (it has more than I’ve included) inside other classes.

I have tried many many different things, but the only thing that works so far is starting a new instance of db in every new class which I think is bad practice. For instance:

class cms {
   function cms(){
       $this->db = new db();
   }

   function is_admin($id) {
    if($this->db->fetch_single_row("SELECT id FROM user WHERE id = ? LIMIT 1", array($id))){
        return true;
    } else {
        return false;
    }
}

in my index.php, I include these classes and use them:

include("db.class.php");
include("cms.class.php");

$cms = new cms();

if($cms->is_admin($id)){
   //code here
}

What is the correct way to accomplish this?

  • 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-08T07:33:51+00:00Added an answer on June 8, 2026 at 7:33 am

    Take a look into the Singleton Design Pattern, it works great for a DB class

    I used to use a class like this for quite a while

       abstract class DB
       {
    
        protected static $instance;
    
        protected $db;
    
        protected static $host = 'host';
        protected static $user = 'user';
        protected static $pass = 'pass';
        protected static $database;
    
        public static function getInstance()
        {
            if (!isset(self::$instance)) self::$instance = new static();
    
            return self::$instance;
        }
    
        protected function __construct()
        {
            $this->db = new PDO(sprintf('mysql:host=%s;dbname=%s', static::$host, static::$database), static::$user, static::$pass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    
        public static function db()
        {
            return static::getInstance()->db;
        }
    
        public function fetch_single_row($sql, $data)
        {
            if( $data !== null )
                $data = array_values( $data ); // Incase it's an associative array
            $sel = self::db()->prepare( $sql );
            $sel->execute( $data );
            $sel->setFetchMode( PDO::FETCH_OBJ );
            $obj = $sel->fetch();
            return $obj;
        }
    }
    

    I would then extend that class for each different database I would need to connect to

    class Main extends DB
    {
        protected static $database = 'db';
    }
    

    You can then use the class like this

    $db = Main::getInstance();
    $obj = $db->fetch_single_row($sql, $data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database class that I made that uses PDO to connect to
I have a class that uses magic methods to store properties. Here is a
Currently, I have a database access class named DB , which uses PDO .
I have a legacy database that uses a table per class hierarchy inheritance strategy
I have a base class that declares a private non-static reference to the DataBase
I have this code that uses NHibernate public bool ValidateUser(string username, string password) {
I have a class that uses linq to access the database. Some methods call
I have a class that inserts information into a MySQL database. I have the
I have a class that saves logging information to the database (in an NServiceBus
I have a class that upon construction, loads it's info from a database. The

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.