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 Answer 1

1

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

Use self::$pdo =

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.