gpt4 book ai didi

尽管增加,PHP 内存限制已耗尽

转载 作者:行者123 更新时间:2023-11-29 00:10:44 25 4
gpt4 key购买 nike

我的网站到处都出现以下错误:

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 600 bytes) in /classes/database.class.php on line 22

我试图通过 ini_set 和 htaccess 增加内存限制,但我仍然遇到同样的错误。

例如,增加到 128M 会导致内部服务器错误。

这是 database.class.php 的第 22 行(及周围):

private $host      = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;

private $dbh;
private $error;
private $stmt;

public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
// line 22 now
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}

最佳答案

如果您多次获取此对象,请像这样更改对象:

public function __construct(){
$options = array(
'dsn' => 'mysql:host=' . $this->host . ';dbname=' . $this->dbname,
'user' => $this->user,
'pass' => $this->pass,
'options' => array( PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
$this->dbh = Db::factory("PDO",$options);
}

然后创建一个数据库工厂类:

final class Db {
protected static $dbInstance;

public static function factory($adapter,$options){
if(!self::$dbInstance){
self::$dbInstance = new $adapter($options['dsn'],$options['user'],$options['pass'],$options['options']);
}

return self::$dbInstance;
}
}

这将有所帮助,并减少不必要的内存浪费和许多创建 PDO 实例的问题。

并在 PHP.ini 中或使用 PHP 更新您的内存限制:

ini_set("memory_limit","128M");

关于尽管增加,PHP 内存限制已耗尽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com