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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:12:26+00:00 2026-05-20T14:12:26+00:00

This may be a question that has been answered before – if so please

  • 0

This may be a question that has been answered before – if so please just leave a comment below and I’ll remove this one.

I have been learning classes in PHP and at the same time making the jump to PDO.

One concept I cant seem to find is how to acomplish the equivalent to this with classes:

config.php

<?php

$host = 'localhost';
$user = 'user';
$pass = 'pass';

$con = mysql_connect($host, $user, $pass) or die("MySQL Error");
mysql_select_db("account_db", $con);

?>

another.php

<?php

require_once('config.php');

$selectStatement = "SELET foo FROM bar";
$selectQuery = mysql_query($selectStatement, $con);

?>

I haven’t quite figured out how I would create a config file/class for a PDO connection and then use it in another class, i.e. Users as below:

<?php

class Users
{
    private $_userId;

    function setUserId($username)
    {
        // Use a predefined database handle to connect to a database to get the users ID - I assume using a preconfigured $dbh handle via an include or extend?
        $sth = $dbh->prepare("SELECT id FROM users WHERE username = :username");
        $sth->bindParam(':username', $username);
        ...
    }
}

?>

Thanks all 🙂

  • 1 1 Answer
  • 1 View
  • 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-05-20T14:12:27+00:00Added an answer on May 20, 2026 at 2:12 pm

    In my projects, I prefer using a class with a static member which holds the PDO object.

    <?php
    class DB
    {
       private static $instance = null;
       public static function get()
       {
           if(self::$instance == null)
           {
               try
               {
                   self::$instance = new PDO('mysql:host=localhost;dbname=name', 'user', 'abc123');
               } 
               catch(PDOException $e)
               {
                   // Handle this properly
                   throw $e;
               }
           }
           return self::$instance;
       }
    }
    

    The I can access it like so:

    <?php
    require 'DB.php';
    class Users
    {
        private $_userId;
    
        function setUserId($username)
        {
            // Using DB::get() to get the PDO object
            $sth = DB::get()->prepare("SELECT id FROM users WHERE username = :username");
            $sth->bindParam(':username', $username);
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

May be this question has been answered before but I couldn't find it. I
I have a question that may have been answered over 9000 times before but
This question has been answered. The problem was that myEventMoveTrainManaul() was being called from
I know this question has been asked and answered before, but none of the
I'm aware that this question may have been asked before, but I still haven't
I'm 10000000% sure that this question has been asked before, however, the majority of
This question has been retitled/retagged so that others may more easily find the solution
This may well be a dumb question and if this has already been answered
You may argue that this question has a legal flavor to it, and that
This may not be the typical stackoverflow question. A colleague of mine has been

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.