dou6495 2012-11-30 18:24
浏览 16
已采纳

致命错误:由于代码错误导致php中允许的内存大小错误

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条回答 默认 最新

  • dongxia1390 2012-11-30 18:34
    关注

    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; 
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能