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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:16:43+00:00 2026-05-23T20:16:43+00:00

i have simple singleton that sets pdo object from factory class , in the

  • 0

i have simple singleton that sets pdo object from factory class , in the first set
the pdo object setes its value just fine . but when i call the singleton second method (that sets the sql call ) i see that the pdo object is not set .
here is the example codes :
the singleton class :

<?php
require_once ('DBFactory.php');
require_once ('Config.php');

class DBHandler {

    private static $instance;
    private $pdo = NULL;

    private function __construct()
    {



    }

    public function ConnectToDb($db_name)
    {
        $ConfigObj = new Config();
        $SelectedDbConfig = $ConfigObj->GetSelectedDB($db_name);
        $pdo = DBFactory::GetDBConnection(Config::DB_TYPE_MYSQL,$SelectedDbConfig["host"],
                                                                 $SelectedDbConfig["db"], 
                                                                 $SelectedDbConfig["user"],
                                                                 $SelectedDbConfig["pass"]);

        if(!isset($pdo))
            return false;

       return true;

    }

    public function SetUserNameAndPass($user,$pass)
    {
        $query_add_user = "INSERT INTO (name ,password) values ('$user' , '$pass') ";
        if(!$this->ExecQuery($query_add_user))
        {
            echo "sql failed";
        }
    }


    public function ExecQuery($query_str)
    {

        $statment_handler = $this->pdo->prepare($query_str);
        if(!$statment_handler) {
            throw new ErrorException($this->pdo->error, $this->pdo->errno);
        }
        if($statment_handler->execute())
            return true;

        return false; 
    }
    public static function GetInstance()
    {
        if (!isset(self::$instance)) {

            $className = __CLASS__;
            self::$instance = new $className;
        }
        return self::$instance;
    }
}

?>

here i set and call the singletone and in the SetUserNameAndPass method im getting
Fatal error: Call to a member function prepare() on a non-object in error
after checking in the debugger i can see that pdo object is empty .

<?php
require_once ('DBFactory.php');
require_once ('Config.php');

class DBHandler {

    private static $instance;
    private $pdo = NULL;

    private function __construct()
    {



    }

    public function ConnectToDb($db_name)
    {
        $ConfigObj = new Config();
        $SelectedDbConfig = $ConfigObj->GetSelectedDB($db_name);
        $pdo = DBFactory::GetDBConnection(Config::DB_TYPE_MYSQL,$SelectedDbConfig["host"],
                                                                 $SelectedDbConfig["db"], 
                                                                 $SelectedDbConfig["user"],
                                                                 $SelectedDbConfig["pass"]);

        if(!isset($pdo))
            return false;

       return true;

    }

    public function SetUserNameAndPass($user,$pass)
    {
        $query_add_user = "INSERT INTO (name ,password) values ('$user' , '$pass') ";
        if(!$this->ExecQuery($query_add_user))
        {
            echo "sql failed";
        }
    }


    public function ExecQuery($query_str)
    {

        $statment_handler = $this->pdo->prepare($query_str);
        if(!$statment_handler) {
            throw new ErrorException($this->pdo->error, $this->pdo->errno);
        }
        if($statment_handler->execute())
            return true;

        return false; 
    }
    public static function GetInstance()
    {
        if (!isset(self::$instance)) {

            $className = __CLASS__;
            self::$instance = new $className;
        }
        return self::$instance;
    }
}

?>



<?php
require_once ('DBHandler.php');

//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$user = ($_GET['user']) ? $_GET['user'] : $_POST['user'];
$password = ($_GET['password']) ?$_GET['password'] : $_POST['password'];


//flag to indicate which method it uses. If POST set it to 1
if($_POST)
{
    $post=1;
}

//Simple server side validation for POST data, of course, you should validate the email
if (!$user) $errors[count($errors)] = 'Please enter your user.';
if (!$password) $errors[count($errors)] = 'Please enter your password.';
$DBhandler_st = NULL;
 if (!$errors) {

     if ($_POST) {
        $DBhandler_st = DBHandler::GetInstance();
        if($DBhandler_st->ConnectToDb("db1"))
        {

            $user = trim($user);
            $password = trim($password);
            $DBhandler_st->SetUserNameAndPass($user,$password);
            echo '1';
        }
        else {
            echo '0';
        }

     }


} else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="admin.html">Back</a>';
    exit;
}

?>
  • 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-05-23T20:16:43+00:00Added an answer on May 23, 2026 at 8:16 pm

    You create local variable $pdo, not set the class variable.

    Use self::$pdo =

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

Sidebar

Related Questions

I have a singleton class called Manager that holds a list of object instances:
I have a simple Invoices class with a Number attribute that has to be
I have a server application (singleton, simple .NET console application) that talks to a
I have a simple pair of classes that looks like this: class X include
I am attempting to create my 1st Simple DLL. I have a class(that's a
In a simple windows setup we have a COM singleton that runs as an
I have a singleton class that is shared by some threads. within a method
I would like to have one (global, singleton) object in my application that exposes
I have a simple Android application that uses an instance of a class, let's
I have a singleton class that stores three NSMutableArrays . Each array stores about

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.