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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:02:53+00:00 2026-06-14T15:02:53+00:00

Assume that I have a class: class pdoc { private static $db_connect_pool; public static

  • 0

Assume that I have a class:

class pdoc {
    private static $db_connect_pool;
    public static function openConnect() {
        try {
            $connect_options_arr = array(PDO::ATTR_PERSISTENT => true);

            self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr);
        } catch (Exception $e) {
            print_r($e);
        }
    }

    public static function getConnection() {
        return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''];
    }

    public static function qry($sql) {
        self::openConnect();
        $db_handle = self::getConnection();
        $st_handle = $db_handle->prepare($sql);
        return $st_handle->execute();           
    }
}

Then, to call the class:

$sql = "SELECT * FROM sometable";

if(pdoc::qry($sql))  echo "y";
else                 echo "n";

Why the code always return n? I have check the connection that has been connected successfully, but while I’ve tried to execute some query, it returns nothing. Any ideas? Thanks.


UPDATE (@Robbie’s code)

class pdoc {
    private static $db_connect_pool;
    public static function openConnect() {
        try {
            $connect_options_arr = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
            self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr);
        } catch (Exception $e) {
            print_r($e);
        }
    }

    public static function getConnection() {
        return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''];
    }

    public static function qry($sql) {
        self::openConnect();
        $db_handle = self::getConnection();
        try {
            $st_handle = $db_handle->prepare($sql);
            $retval = $st_handle->execute(); //--> Got error on this line       
        } catch (Exception $e) {
            Die('Need to handle this error. $e has all the details');
        }
        return $retval;  
    }
}

The error said: exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'.


ANSWER

Change:

... new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", ...

into:

... new PDO("".DB_DRIVER.":host=".DB_HOST.";dbname=".DB_NAME."", ...

After catch the error message (from the updated code) and referring to this thread, I found that dbname part has written as dn_name on my code. So I change it into dbname and it works perfect! Thanks @Robbie for your code! 🙂

  • 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-14T15:02:54+00:00Added an answer on June 14, 2026 at 3:02 pm

    If it returns nothing, then it failed. You need to call the error functions (errorInfo, errorCode) to find out why.

    But the tricky part is you don’t know if the error is on the database or the statement, so the best trick is to use exception error reporting, wrap all the functions in a try catch, and the exception, when trapped, will belong to either the DB or the statement. Much easier to handle.

    Your code would be:

    class pdoc {
        private static $db_connect_pool;
        public static function openConnect() {
            try {
                $connect_options_arr = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
                self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr);
            } catch (Exception $e) {
                print_r($e);
            }
        }
    
        public static function getConnection() {
            return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''];
        }
    
        public static function qry($sql) {
            self::openConnect();
            $db_handle = self::getConnection();
            try {
                $st_handle = $db_handle->prepare($sql);
                $retval = $st_handle->execute();            
            } catch (Exception $e) {
                Die('Need to handle this error. $e has all the details');
            }
            return $retval;  
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that we have the following code: class Program { static volatile bool flag1;
Assume some domain and view objects (that have no common base class) public class
Assume I have a class that looks like this: class Sample { public string
Assume that we have class public class RMenuItem { public List<RMenuItem> ChildrenItems { get;
Assume that I have a class that exposes the following event: public event EventHandler
Assume, that I have class with static methods only. Will class loader load every
Assume that I have the following object public class MyClass { public ReadOnlyDictionary<T, V>
Assume that I have three class; UserAccount, UserGroup and Role. UserGroup and Role are
Assume that I have a particular class of object that defines a class method
So let's assume I have a class named ABC that will have a list

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.