Solved

PHP5: when do I close my database object connection?

Posted on 2014-11-04
3
97 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
[X]
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
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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

730 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