MySQL Forums
Forum List  »  PHP

How Many Connections are crating here
Posted by: Abdullah Rashid
Date: December 17, 2016 10:41PM

Below are some file structure of my website. I have removed unnecessary codes from these example codes in order to avoid confusions and larger code chunk.

class: connection.php (collected from internet)

<?php
class connection {
private $_connection;
private static $_instance; //The single instance
private $_host = "localhost";
private $_username = "username";
private $_password = "password";
private $_database = "database";

/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}

// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
}

// Magic method clone is empty to prevent duplication of connection
private function __clone() { }

// Get mysqli connection
public function getConnection() {
return $this->_connection;
}
}
?>

class: login.php (this class authorize users in the website)

class login extends connection{
private $conn;
private $userid;
public $loggedin;

function __construct($id){
$this->userid = $id;
$this->set_login();
}

private function set_login(){
$this->connect();
$result = $this->conn->query("SELECT status FROM users WHERE userid='".$this->userid.'"');
if($result){
$this->loggedin = true;
}else{
$this->loggedin = false;
}
}

private function connect(){
$this->conn = parent::getInstance()->getConnection();
}
}


phppage: index.php (this page is for testing number of connection)

<?php
spl_autoload_register(function ($class) {
include '../classes/' . $class . '.php';
});
$user = new login("75");
if($user->logged_in){
$db = connection::getInstance();
$conn = $db->getConnection();
$query = 'SHOW STATUS WHERE variable_name LIKE "Threads_%" OR variable_name = "Connections"';
$result = $conn->query($query);
while($row=$result->fetch_assoc()){
echo $row['Variable_name'].' - '.$row['Value'].'<br />';
}
}
?>

the index.php will show the following information about connection:

Connections - 1026572
Threads_cached - 7
Threads_connected - 9
Threads_created - 42943
Threads_running - 2

I am referencing two `$conn` in the above examples, one is inside `login.php` class and the another is in `index.php` page, but from the same `connection.php` class.

I can know the total active connection from `Threads_running` value. But sometimes I can see that the `Threads_running - 2` and sometimes it becomes `Threads_running - 1` while I reload the page.

So, my question to expert is, according to the classes above, is there any possibility to have more than one database connection at a time?

If no, why it is showing `Threads_running - 2` sometimes?

Options: ReplyQuote


Subject
Written By
Posted
How Many Connections are crating here
December 17, 2016 10:41PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.