Solved

PHP5: when do I close my database object connection?

Posted on 2014-11-04
3
92 Views
Last Modified: 2014-11-04
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,
0
Comment
Question by:epifanio67
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 40422900
You don't need to, they close when the page finishes execution.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40422903
Don't even bother.  The PHP "cleanup" routines will take care of that for you.
0
 

Author Closing Comment

by:epifanio67
ID: 40422924
Thank you experts... just wanted to make sure...

Regards,
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question