Hi,
I believe there is a limitation on maximum user connections to MySQL database at most hosting services. If I'm correct, it's usually set to 3 (3 users can connect to the database at the same time??).
My question is what does this exactly mean? Does mysql_connect() look for an open link identifier within that script (meaning within the execution of a script) ? If so, what happens if more than 3 people request a connection at a same time (doesn't this reach the max user connection limit therefore break??)
I looked into the manual from PHP.net and it explains that mysql_connect() searches for an open link identifier first, so that it wouldn't open an extra connection but where is this link identifier exactly stored in?
To test this, let's say that you have 3 different classes that require mysql_connect() to run queries. There is also a database class that does almost everything for you (connect, disconnect, run query, etc.) Each class stores this database object in its property when it is instaciated.
so
class database
{
// Storage for a link identifier
var $resource;
// Constructor
function database()
{
...
}
// Connects to the database
function connect()
{
$this->resource = mysql_connect(..,..,..,);
}
...
...
... Rest of class definition follows...
}
class class1 (or class2, or class3)
{
// class property
var $db;
// constructor
function class1(&$db)
{
$this->db = $db;
}
...
...
... Rest of class definition follows...
}
$db = new database(); // Instanciate a database object
$class1 = new class1($db);
$class2 = new class2($db);
$class3 = new class3($db);
The above example shows that the class constructor recieves a $db parameter by reference, but it copies that to its $db property, so when $this->db->connect() is called, each class (class1, class2, class3) opens a new database link since they don't share a same link identifier stored in $db->resource; therefore it easily reaches the user connection limit (am I understanding this right??)
If the line:
$this->db = $db; // $this->db copies $db
is like this:
$this->db = &$db; // $this->db stores $db by reference
If $db parameter is stored in class property by reference, all classes share a same object (same memory allocation, resource, etc) so it looks into a same link identifier, therefore if one connection is already opened, all classes share a same connection??
It's very hard to explain, if you have the answer, please let me know!
Thanks!