Link to home
Start Free TrialLog in
Avatar of epifanio67
epifanio67

asked on

PHP5: when do I close my database object connection?

Hello Experts,

In the sample database code below, when do I close the database obj connection?
I use the object to query the db throughout the session....

//***** index.php 

require_once 'Database.php';

$db = new Database();
$db->getInstance();

$db->testQuery();
$email = $db->testQuery();


//***** Database.php

class Database extends mysqli_stmt {

	//set db access data
	private $server = "localhost";
	private $user = "test";
	private $pass = "test";
	private $db = "test";
	
	private $instance;

	function getInstance(){
	
		$db = new mysqli ( $this->server, $this->db, $this->pass, $this->user );
	
		if ($db->connect_error) {
			die('Connect Error (' . $instance->connect_errno . ') ');
		} else {
			$this->instance = $db;
		}		
	
		return $this->instance;
	}
	
	function testQuery( $name ){
		
		$stmt =  $this->instance->stmt_init();
		
		$query = "SELECT `email` FROM customers WHERE Name=?";
		if ($stmt->prepare($query)) {		
			$stmt->bind_param("s", $name);
			$stmt->execute();
			$stmt->bind_result( $email );
			$stmt->fetch();
			$stmt->close();
		}
		
		return $email;
		
	}
	
	function dbClose(){
		$this->instance->close();
	}
	
	
}//END OF CLASS...

Open in new window


Regards,
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of epifanio67
epifanio67

ASKER

Thank you experts... just wanted to make sure...

Regards,