Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP5: when do I close my database object connection?

Posted on 2014-11-04
3
96 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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