/    Sign up×
Community /Pin to ProfileBookmark

MySQL Error HY0000/1040

So I’m building an OOP project for the first time.

I have a class called DBH (database handler). Here I have private properties for my connection and a public method called “connect”.

This method returns a connection.

[code]
$con = new mysqli($this->servername, $this->username, $this->password, $this->dbname);

return $con;
[/code]

all over my website I call other classes and methods which use this, in each method I have a connection like this

[code]
$Db = new Dbh;
$con = $Db->connect();
[/code]

I don’t ever close it.

occasionally I get the above error, I’m not sure if it’s my code and too many connections or if it’s my shared hosting.

is there anything I should do to improve my side of things?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 23.2021 — Yeah, looks like that's a "too many connections" error. Generally you only ever need one connection, since (normally) PHP does not have parallel processes. My preferred technique is to instantiate one instance of my DB class, then pass it in to each class that might need it (typically via its __construct() method -- or just pass the actual connection if that makes more sense?
[code=php]
<?php

class = example {
private $con;
public function __construct(mysqli $con, $other, $args) {
$this->con = $con;
}
}
[/code]

Then in your main script:
[code=php]
<?php

$Db = new Dbh;
$con = $Db->connect();

$example = new Example($con, 'other', 'arg');
$example_2 = new Example($con, 'somthing', 'else');
[/code]
Copy linkTweet thisAlerts:
@kiwisauthorOct 23.2021 — @NogDog#1638458

so in that example class. Is that the DBh class or another?

assume it’s another. If so and in an imaginary scenario where the class is a car class. When I construct it aren’t I setting the make, mileage, yellow etc.

then in another method I’m saving it to the database but only when called.

How would that work?
Copy linkTweet thisAlerts:
@NogDogOct 23.2021 — So in your Car class, you'd have access to the mysqli object as a class variable.
[code=php]
class Car {
private $con;
public function __construct(mysqli $con) {
$this->con = $con
}
public function get_by_make($make) {
$sql = "SELECT * FROM cars WHERE make = ?";
$stmt = $this->con->prepare($sql);
$stmt->bind_param('s', $make);
$stmt->execute();
// then fetch whatever you want in whatever format, then return it
}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogOct 23.2021 — PS: I'd probably try to come up with a more explicit name than $con, even if it's just $dbCon or $db_con. :)
Copy linkTweet thisAlerts:
@kiwisauthorOct 23.2021 — @NogDog#1638460

right, so every time I initiate the car class I need to pass in the $con?

$car = new Car($con);

When I create a car from user variables such as a form post. Do I just make this via a method such as public function create()?

I'm currently doing that via the construct method. But your idea has me passing in the connection
Copy linkTweet thisAlerts:
@SempervivumOct 23.2021 — Another option to make sure that there is only one instance of the database connection is using a singleton:

https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php

Benefit: You would need to change your class Dbh only, creating the instance and connecting
``<i>
</i>$Db = new Dbh;
$con = $Db-&gt;connect();<i>
</i>
``

could be left unchanged.
Copy linkTweet thisAlerts:
@kiwisauthorOct 23.2021 — @Sempervivum#1638464

that looks like a really good option.

could the same logic not be applied to lists of cars too. Like if I had a function getAllCars

could I make that single?

the only issue would be how go capture updates if they were loaded
Copy linkTweet thisAlerts:
@dsgufOct 23.2021 — body{

background-color: pink;

}

#Titolo{

font-family: "JetBrains Mono";

font-size: 66px;

color:blue;

text-align: center;

}

table{

border-collapse: collapse;

margin-left: auto;

margin-right: auto;

}

td.nobordi{

color:white;

}

td{

height:50px;

width:50px;

border: 1px solid blue;

background-color: gainsboro;

text-align: center;

font-family: "JetBrains Mono";

color:blue;

}

.nobordi{

border: none;

background-color: pink;

}

.indicatore{

color: red;

font-size: 20px;

}

**TRY THIS ON A CSS FILE BROSKY**
Copy linkTweet thisAlerts:
@kiwisauthorOct 23.2021 — @Sempervivum#1638464

<i>
</i>class dbh{

<i> </i>private $servername;
<i> </i>private $username;
<i> </i>private $password ;
<i> </i>private $dbname ;
<i> </i>private $charset;

<i> </i>private static $dbCon = null;
<i> </i>
<i> </i>private function __construct(){
<i> </i> $this-&gt;servername = "localhost";
<i> </i> $this-&gt;charset = "utf8mb4";
<i> </i> $this-&gt;username = "ABC";
<i> </i> $this-&gt;password = "ABC";
<i> </i> $this-&gt;dbname = "ABC";
<i> </i> $con = new mysqli($this-&gt;servername, $this-&gt;username, $this-&gt;password, $this-&gt;dbname);
<i> </i> return $con;
<i> </i>}
<i> </i>public static function connect(){
<i> </i>
<i> </i> if (self::$dbCon == null){
<i> </i> self::$dbCon = new dbh();
<i> </i> }
<i> </i> return self::$dbCon;

<i> </i>}
<i> </i>



My Code

<i>
</i>$Dbh = new Dbh;


Error

Fatal error: Uncaught Error: Call to private dbh::__construct() from invalid context in....
Copy linkTweet thisAlerts:
@TakigoOct 23.2021 — Today I will show your problem to a colleague who understands firmware development and coding. I think he can help you solve the problem.
Copy linkTweet thisAlerts:
@SempervivumOct 23.2021 — OOP is not my strongest suit, however by reading the docs and tuts I figured out this code:
``<i>
</i>class Dbh
{

private $servername;
private $username;
private $password;
private $dbname;
private $charset;

private static $dbCon = null;

public function __construct()
{
$this-&gt;servername = "localhost";
$this-&gt;charset = "utf8mb4";
$this-&gt;username = "un";
$this-&gt;password = "pw";
$this-&gt;dbname = "testdb";
if (self::$dbCon == null) {
self::$dbCon = new mysqli($this-&gt;servername, $this-&gt;username, $this-&gt;password, $this-&gt;dbname);
}
}
public static function connect()
{
return self::$dbCon;
}
}<i>
</i>
``

Note that there is no return value for the constructor:

https://stackoverflow.com/questions/11904255/constructor-returning-value
Copy linkTweet thisAlerts:
@TakigoOct 23.2021 — I am not good at OOP and [firmware development](https://sirinsoftware.com/services/firmware_development/) issues. But the code above looks quite working and may help you solve the issue.
Copy linkTweet thisAlerts:
@NogDogOct 23.2021 — Okay, rambling off into more theoretical issues... :)

Singleton can work, but I tend to avoid it as it makes testing a bit trickier by embedding (and effectively hiding) the dependency on it inside of each class where it's used, whereas passing it into each such class's constructor makes it part of the public interface (and easier to stub out during unit tests, or replace with a new class, etc.).

On a side note, if your DB class is essentially there to wrap up your DB config stuff (host, user, password, config options,...), you may want to consider simply having it _extend_ the mysqli class. Then just pass that instantiated class into each model that needs it, and that code then just treats it as a mysqli object.
[code=php]
<?php
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();
[/code]
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...