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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:13:51+00:00 2026-05-20T10:13:51+00:00

I am new to PDO and I have pretty simple question. I have a

  • 0

I am new to PDO and I have pretty simple question. I have a simple function for connecting to DB:

function connectDB()
    {
        try {
            $dbh = new PDO('mysql:host='.Config::$db_server.';dbname='.Config::$db_name, Config::$db_login, Config::$db_password, array(
                PDO::ATTR_PERSISTENT => true
            ));
            $dbh->exec("SET CHARACTER SET utf8"); 
            $dbh = null;
        } catch (PDOException $e) {
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        }
    }

After calling this function I successfully connect to db. Later when trying to send a query using $dbh->query I got “Call to a member function query() on a non-object “. I do understand that – I don’t have an instance of the class at the moment. But the only think to achieve that is to use $dbh = new PDO(“settings”) again, which is kind of stupid isn’t? The function has no sense than. I tried to return the $dbh in the connectDB function (before the NULL statement) but it wasn’t really working.

How should be this done properly?

  • 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-20T10:13:51+00:00Added an answer on May 20, 2026 at 10:13 am

    It depends on your app’s architecture, but I believe, you should make database handle a class variable, initialize it in constructor and use it later.

    class DatabaseAccess{
        private $_db;
        public function __construct(){
            try {
                $this->_db = new PDO('mysql:host='.Config::$db_server.';dbname='.Config::$db_name, Config::$db_login, Config::$db_password, array(
                    PDO::ATTR_PERSISTENT => true
                ));
                $this->_db->exec("SET CHARACTER SET utf8"); 
                //notice I removed "= null" part
            } catch (PDOException $e) {
                print "Error!: " . $e->getMessage() . "<br/>";
                die();
            }
        }
    
        public function getSomething(){
            //run your query here:
            return $this->_db->query('');
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this PHP PDO snippet work? $DBH = new PDO(mysql:host=$host;dbname=$dbname, $user, $pass); $STH->bindParam(':fftb',
I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname='
I have a class with this in self::$DB = new PDO(mysql:dbname=$dbname;host:=127.0.0.1 , 'root' ,
I have the following code: $link = new PDO(mysql:dbname=$databasename;host=127.0.0.1,$username,$password); $query = $link->prepare(SELECT * FROM
I have the following relevant code: try { self::$db = new PDO($dsn, self::USER, SELF::PASS);
I have a very simple test script: <?php $DSN = "mysql:host=db.example.edu;port=3306;dbname=search_data"; try { $DB
I have the following code: public static function check($ip,$op,$page) { $con = new PDO(DBN,DB_USER,DB_PASS);
I'm kinda new to PDO with MYSQL, here are my two files: I have
$dbo = new PDO(mysql:host=localhost;dbname=database, databaseuser, databasepassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => SET NAMES utf8)); $dbo -> exec(set
I connected to database with PDO and state SET NAMES: $db = new PDO('mysql:host=localhost;dbname=gs',

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.