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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:42:12+00:00 2026-06-15T03:42:12+00:00

I am quite familiar with PHP but I have just started doing a bit

  • 0

I am quite familiar with PHP but I have just started doing a bit of object oriented stuff with it. I wanted to make a singleton database connection but I ran into a problem and error.”Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 523800 bytes) in”
I know that I am not supposed to run out of memory the query I am running is

$con = getConnection();
$stmt = $con->prepare("SELECT gene_name,jgi_protein_id FROM   jgi_transcriptid_proteinid_match where our_protein_id = ?");

Here is the code for the class.

class Connection
{
    // Store the single instance of connection 
    private static $connection;

    private function __construct()
    {
        $connection = new mysqli(HOSTNAME, DBUSER, PASSWORD, DBNAME);

        if ($connection->connect_errno)
            die("Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error);
    }

    public static function getInstance() 
    { 
        if (!self::$connection) 
            self::$connection = new Connection(); 

        return self::$connection; 
    }

    public function prepare($query) 
    {
        $statement = $this->prepare($query);
        return $statement; 
    }
}

I am using mysqli for the database stuff.

  • 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-15T03:42:13+00:00Added an answer on June 15, 2026 at 3:42 am

    There are a couple of issues in this code:

    Infinite recursion

    public function prepare($query) 
    {
      $statement = $this->prepare($query);
      return $statement; 
    }
    

    Referencing a local variable, instead of the static one
    The code should probably reference self::$connection. Based on the class, though, I’m not sure, as self::$connection is used in a different way in getInstance(), which I don’t see called anywhere.

    private function __construct()
        {
            $connection = new mysqli(HOSTNAME, DBUSER, PASSWORD, DBNAME);
    
            if ($connection->connect_errno)
                die("Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error);
        }
    

    Naming confusion
    The class is called Connection, and it contains a static variable named $connection, which stores the singleton instance of the class. The constructor contains another $connection, which is instead a mysqli connection.

    Refactored class – UNTESTED FIX
    The class below has not been tested and is provided for illustrative purpose. Use at your own risk.

    class Connection {
    // Store the single instance of the class
    private static $instance;
    // Store the mysqli connection
    private $connection;

    public function __construct() {
        // NOTE: it would be better to pass connection parameters as arguments
        $this->connection = new mysqli(HOSTNAME, DBUSER, PASSWORD, DBNAME);
    
        if ($this->connection->connect_errno)
            die("Failed to connect to MySQL: (" . $this->connection->connect_errno . ") " . $this->connection->connect_error);
    }
    
    public static function getInstance() { 
        if(empty(self::$instance)) {
            self::$instance = new Connection(); 
        }
    
        return self::$instance; 
    }
    
    public function prepare($query) {
        $statement = $this->connection->prepare($query);
        return $statement; 
    }
    

    }

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

Sidebar

Related Questions

I have been a desktop developer for a few years mostly doing object oriented
To be frank, I am quite new to object oriented programming. I'm more familiar
I was quite familiar with the V2 api, but it's been a while since
I have a fairly simple problem for someone familiar with js. I quite like
I'm quite familiar with the System.Diagnostics.Process class. But, I'm wondering about how I can
I'm quite familiar with php and when submitting variables with data values on a
I have a great idea for an Android app, but as I'm only familiar
This topic maybe sound familiar but im not quite pleased with any of the
I'm quite familiar with PHP dockblocks since it's been my job for the last
I am extremely new to PHP and, although I am quite familiar to javascript,

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.