I have db
class that nearly all classes are extending:
我有db类,几乎所有类都在扩展:
class db {
protected $db;
public function __construct() {
$this->connect();
}
protected function connect() {
$this->db = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->db->error); (line 22)
$this->db->set_charset('utf8');
}
}
Here is page class
这里是页面类
class page extends db {
var $common;
public function __construct() {
parent::__construct();
$this->common = new common();
And
和
class common extends db {
public function __construct() {
parent::__construct();
}
I'm getting
我得到
Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): User admin already has more than 'max_user_connections' active connections in /home/tural/public_html/incl/classes/class.db.php on line 22
警告:mysqli:mysqli(mysqli()。(42000/1203):用户管理员在/home/tural/public_html/incl/classes/class.db中已经有超过“max_user_connections”的活动连接。php在第22行
How can I fix this problem?
我如何解决这个问题?
2 个解决方案
#1
4
Every class inheriting from db
you instantiate establishes a new database connection. You should have just one DB class instance. All the page
and common
don't need to inherit from it, just pass them a db
instance.
从实例化的db继承的每个类都建立一个新的数据库连接。您应该只有一个DB类实例。所有的页面和公共页面都不需要继承,只需向它们传递一个db实例。
#2
0
When you extend db class, a mysqli connection is made after each instance generation. I think it would be better if you define class db as singleton
扩展db类时,在每个实例生成之后都会进行mysqli连接。我认为如果您将类db定义为单例类会更好
code is the following ;
代码如下;
class db {
private static $_db; // db instance
protected $connection;
private function __construct() {
$this->connection = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->connection->error);
$this->connection->set_charset('utf8');
}
public function get_instance()
{
if( isset( self::$_db) )
{
return self::$_db;
}
else
{
self::$_db = new db();
}
return self::$_db;
}
}
By this way, you can only create 1 mysql connection.
通过这种方式,您只能创建一个mysql连接。
You can reach db class by db::get_instance()->do_something();
可以通过db::get_instance()->do_something()到达db类;
the common class can be like this;
公共类可以是这样的;
class page {
var $common;
var $db;
public function __construct() {
parent::__construct();
$this->db = db::get_instance();
$this->common = new common();
}
}
I think this is better design.
我认为这是更好的设计。
#1
4
Every class inheriting from db
you instantiate establishes a new database connection. You should have just one DB class instance. All the page
and common
don't need to inherit from it, just pass them a db
instance.
从实例化的db继承的每个类都建立一个新的数据库连接。您应该只有一个DB类实例。所有的页面和公共页面都不需要继承,只需向它们传递一个db实例。
#2
0
When you extend db class, a mysqli connection is made after each instance generation. I think it would be better if you define class db as singleton
扩展db类时,在每个实例生成之后都会进行mysqli连接。我认为如果您将类db定义为单例类会更好
code is the following ;
代码如下;
class db {
private static $_db; // db instance
protected $connection;
private function __construct() {
$this->connection = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->connection->error);
$this->connection->set_charset('utf8');
}
public function get_instance()
{
if( isset( self::$_db) )
{
return self::$_db;
}
else
{
self::$_db = new db();
}
return self::$_db;
}
}
By this way, you can only create 1 mysql connection.
通过这种方式,您只能创建一个mysql连接。
You can reach db class by db::get_instance()->do_something();
可以通过db::get_instance()->do_something()到达db类;
the common class can be like this;
公共类可以是这样的;
class page {
var $common;
var $db;
public function __construct() {
parent::__construct();
$this->db = db::get_instance();
$this->common = new common();
}
}
I think this is better design.
我认为这是更好的设计。