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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:55:05+00:00 2026-05-26T22:55:05+00:00

I instantiated a PDO object instead of a mysqli object in my database class.

  • 0

I instantiated a PDO object instead of a mysqli object in my database class. Obviously my mysqli functions no longer work. How do I update this line.

$row=mysqli_fetch_row(database::query($query));    

one_db::$db holds the pdo resource…query is just…

public function query($query) 
{
    return one_db::$db->query($query);
}

Actually I’m not sure if this needs updating as well to work with PDO…

Here is my PDO instantiation, I left in the mysqli for comparison

private function __construct() 
{
    self::create_pdo_object();
}

private function create_pdo_object()
{
    try {
        self::$db = new PDO(DB_DRIVER, DB_USER, DB_PASS);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
}

private function create_mysqli_object()
{
    self::$db=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);   
}
  • 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-26T22:55:06+00:00Added an answer on May 26, 2026 at 10:55 pm

    It looks like you might ought to consider using the factory pattern. Unfortunately, in your case, I don’t want to rewrite your entire class so we’re gonna use a modified factory pattern. Create a function named factory() like so:

    public static function factory($type = 'mysqli')
    {
        switch ($type) {
            case 'pdo':
                return self::create_pdo_object();
            case 'mysqli':
            default:
                return self::create_mysqli_object();
        }
    }
    

    Incidentally, if you’re going to use self then your functions should be declared static. In which case, it’s considered best practice to prefix them with underscores. And if you’re gonna go that route, then you might as well follow the convention for camel casing function names. (although, it could be argued that those functions should be public in case you don’t want to use factory, in which case you should omit the underscore prefix).

    Also, your __construct() function should have the public access modifier. You could call factory() from within your constructor, but they are static methods using a static class member (self::$db), so instantiating an object is kind of pointless. Nevertheless, here is the code:

    public function __construct($type = 'mysqli') 
    {
        self::factory($type);
    }
    
    public static function factory($type = 'mysqli')
    {
        switch ($type) {
            case 'pdo':
                // return self::createPdoObject();
                // or, if private  
                return self::_createPdoObject();
            case 'mysqli':
            default:
                // return self::createMysqliObject();
                // or, if private  
                return self::_createMysqliObject();
        }
    }
    
    // public static function createPdoObject()
    // or
    private static function _createPdoObject()
    {
        try {
            self::$db = new PDO(DB_DRIVER, DB_USER, DB_PASS);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }
    
    // public static function createMysqliObject()
    // or
    private static function _createMysqliObject()
    {
        self::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);   
    }
    

    As far as fetching a row with PDO, you would need to alter the code to something like:

    if ($db instanceof PDO) {
        $query = $db->prepare($sqlSelectString);
        $query->execute();
    
        // or use PDO::FETCH_ASSOC if you want arrays instead
        $result = $query->fetch(PDO::FETCH_OBJ);
    }
    

    If this code exists in your db class, then you would change $db to self::$db, but I’m not sure where you would want to put that code..

    For more information on querying using PDO, check out this page.

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

Sidebar

Related Questions

Given an instantiated JavaScript object/class can I make an Ajax call from that class
I have instantiated an object like this: GraphMatrixDirected<String, Integer> g g is passed to
I am trying to instantiate a PDO object like this: $this->pdo['pdo'] = new PDO('mysql:host=127.0.0.1;dbname=mydb;charset=UTF-8',
1) OnCreate is public method of instantiated object from ClsLast class. But I wanted
I have a wrapper class (BluetoothDiscoverer) which is instantiated within a Service. This class
this here below is my class database in php <?php class DB { private
I'm using a readonly dictionary object in a class instantiated with a Singleton lifestyle
When an object is instantiated in Java, what is really going into memory? Are
When an object is instantiated in Java, is it bound to the thread that
The object I’m working on is instantiated in JavaScript, but used in VBScript. In

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.