Can anyone help me? I’m getting this error frequently. My website is made up of multiple classes with methods which all require a DB query.
when I load a page I get the person as per this example, this includes a database query. when I get specific info like last meetings I get do another query of the database to get this information.
I don’t get this all the time but it pops up for time to time.
im wondering if I’m doing it to myself with too many connections or if there’s a better way to do my connection? Pass it into ever method for example?
currently I create an instance of the Database class and connect to it when I need it.
[code] Warning: mysqli::__construct(): (HY000/1040): Too many connections in /home2/myfolder/my site.com/db.php on line 119
Warning: mysqli::prepare(): Couldn’t fetch mysqli in /home2/myfolder/my site.com/index.php on line 37
Fatal error: Uncaught Error: Call to a member function bind_param() on bool in /home2/myfolder/my site.com/index.php:38 Stack trace: #0 /home2/myfolder/public_html/my site.com/index.php(9): Person->getById(‘368’) #1 {main} thrown in /home2/myfolder/my site.com/index.php on line 38
>@kiwis80#1639991 im wondering if I’m doing it to myself with too many connections or if there’s a better way to do my connection?
>if there’s a better way to do my connection?
__construct()
[code=php]
class Example {
private $db;
public function __construct(mysqli $db) {
$this->db = $db;
}
public function something() {
// use $this->db when you need the mysqli connection
}
}
[/code]
<i>
</i> private function __construct(){
$this->servername = "localhost";
$this->charset = "utf8mb4";
$this->username = "username";
$this->password = "password";
$this->dbname = "dbname";
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
<br/>
<i> </i>}
<i> </i>public static function connect(){
<i> </i>
<i> </i> if (self::$instance == null){
<i> </i> self::$instance = new dbh();
<i> </i> }
<i> </i> return self::$instance;
<i> </i>}
<i> </i>public function getConnection() {
<i> </i> return $this->conn;
<i> </i>}
<i> </i>
<i>
</i> $Db = dbh::connect();;
$con = $Db->getConnection();
<i>
</i>$secondClass = new secondClass($this->dbh);
<i>
</i>class dbh extends mysqli {
private $servername = "localhost";
private $username = "ABC";
private $password = "ABC";
private $dbname = "ABC";
public function __construct() {
// call the actual mysqli constructor:
parent::__construct($this->servername, $this->username, $this->password, $this->dbname);
}
}
class Example {
private $dbh;
public function __construct(dbh $dbh) {
$this->dbh = $dbh
}
public do_something() {
$sql = 'select * from something';
$this->dbh->query($sql);
// etc...
}
}
// do stuff...
$dbh = new dbh();
$test = new Example($dbh);
$test->do_something();
``<i>
</i>include 'includes/load-classes.php';
$person = new Person($database);
$car = new Car($database);<i>
</i>
`</CODE>
load-classes.php
<CODE>
`<i>
</i>require_once '/classes/Dbh.php';
foreach (glob("/classes/*.php") as $filename){
include_once $filename;
}
$database = new database();<i>
</i>
``
PHP Warning: mysqli::__construct(): (HY000/1040): Too many connections in /home2/user/classes/Dbh.php
PHP Warning: mysqli::prepare(): Couldn't fetch database in /home2/user/classes/carClass.php on line 103
``<i>
</i>$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";<i>
</i>
``
``<i>
</i>require_once '/classes/Dbh.php';
foreach (glob("/classes/*.php") as $filename){
include_once $filename;
}
$database = new database();<i>
</i>
``
``<i>
</i>if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";<i>
</i>
``
> Connection failed: Too many connections
``<i>
</i>class database extends mysqli {
private $servername = "localhost";
private $username = "admin";
private $password = "password";
private $dbname = "dbname";
public function __construct() {
echo parent::__construct($this->servername, $this->username, $this->password, $this->dbname);
}
}<i>
</i>
``
class database extends mysqli <br/>
{<br/>
private $servername = "localhost";<br/>
private $username = "admin";<br/>
private $password = "password";<br/>
private $dbname = "dbname";<br/>
public function __construct() <br/>
{<br/>
echo "Attempting to make connection<br>";<br/>
$db = parent::__construct($this->servername, $this->username, $this->password, $this->dbname);<br/>
echo "Finished making connection<br>";<br/>
return $db;<br/>
}<br/>
}
``<i>
</i>class database extends mysqli {
private $servername = "localhost";
private $username = "admin";
private $password = "pass";
private $dbname = "db";
public function construct() {
echo "Attempting to make connection<br>";
$db = parent::construct($this->servername, $this->username, $this->password, $this->dbname);
echo "Finished making connection<br>";
return $db;
}
}
$dbh = new database();
var_dump($dbh);<i>
</i>
``
> object(database)#1 (6) { ["client_info"]=> string(14) "mysqlnd 7.4.29" ["client_version"]=> int(70429) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" }
$dbh = new database();
``<i>
</i>ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class database extends mysqli {
private $servername = "localhost";
private $username = "admin";
private $password = "pass";
private $dbname = "dbname";
public function construct() {
echo "Attempting to make connection<br>";
exit();
$db = parent::construct($this->servername, $this->username, $this->password, $this->dbname);
echo "Finished making connection<br>";
return $db;
}
}
echo 'Test Page.';
$dbh = new database();<i>
</i>
``
public function construct() {
public function __construct() {
__construct()
return $db;
new
> Attempting to make connection
> Finished making connection
``<i>
</i> public function __construct() {
echo "Attempting to make connection<br>";
$db = parent::__construct($this->servername, $this->username, $this->password, $this->dbname);
echo "Finished making connection<br>";
return $db;
}<i>
</i>
``
``<i>
</i> public function __construct(database $database){
$this->dbh = $database;
}<i>
</i>
``
> Attempting to make connection
>
> Warning: mysqli::__construct(): (HY000/1040): Too many connections in /home2/user/public_html/mydomain.com/db-test.php on line 14
> Finished making connection
>@ginerjm#1644999 Besides I don't use classes
0.1.9 — BETA 4.18