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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:27:32+00:00 2026-06-15T21:27:32+00:00

I am working on a subquery library for CodeIgniter ( link to project ),

  • 0

I am working on a subquery library for CodeIgniter (link to project), which works by returning database objects that you can use instead of the normal $this->db object.

Originally, I was making new database objects each time:

$this->CI =& get_instance();
$db = $this->CI->load->database('', true);

But I then realized (or rather someone pointed out to me) that this would make a new database connection each time! So I decided to try to clone objects instead of making new ones.

I updated the code to look like so (link to full code):

class Subquery{
    var $CI, $db;

    function __construct(){
        $this->CI =& get_instance();
        $this->db = $this->CI->db; // Default database connection
    }

    function start_subquery(){
        $newDatabase = clone $this->db;
        // some more code
        return $newDatabase;
    }
}

I tested this, and it seems to work, but I’m not sure if this solves the problem. Does using clone make a new database connection, or does it use references internally and only keep one connection?

  • 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-15T21:27:33+00:00Added an answer on June 15, 2026 at 9:27 pm

    First off, the =& is unnecessary and potentially harmful if you are not using PHP 4 (I assume get_instance returns an object).

    As for whether clone creates a new connection .. this depends on the DB implementation. As far as I can tell, PDO does not create an additional connection when it’s cloned. If you want to test this, create a small script that runs start_subquery to some effect and then sleeps for a decent amount of time. Connect to mysql and run:

    SHOW STATUS WHERE `variable_name` = 'Threads_connected'
    

    If it’s more than 2 (your connection to run the query and the one from your script) then an additional connection is being made for some reason.

    As for whether you should clone the DB, I don’t know why you would want to, but I don’t really know the use case for this class nor do I know much about CI.

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

Sidebar

Related Questions

I've got three objects (splistitemcollection) that im joining together which is working great but
Working with an undisclosed API, I found a function that can set the number
I'm currently working on a database which is rather complex, at least for my
Working on the problems on Project Euler to try to learn Clojure. I'm on
Working on a project using Entity Framework (4.3.1.0). I'm trying to figure out how
I'm working on a web service that fetches data from an oracle data source
As per https://stackoverflow.com/questions/3264227/relations-with-multiple-keys-in-doctrine-1-2 , I have two tables which (as I can't get it
I have a working query that gives me the maxdate: SELECT a.client_id AS client_id
I am working with the following query.I want to use Date_Sent_to_Recorder in my other
I’m struggling to improve a n+1 query on a project I’m working on. I

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.