simple question about mysql_close()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bytte
Forum Commoner
Posts: 75
Joined: Sun Nov 23, 2003 8:20 am
Location: Belgium

simple question about mysql_close()

Post by bytte »

As the title says, this is a simple question.

If I open the same mysql database a few times on a PHP page, should I close it everytime after I made a connection?

Or is it sufficient to put mysql_close() at the end of the page to close all open connections to that same database?
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Post by Phoenixheart »

But why do you have to do several mysql_connect() on just one page? I think one connect is enough?
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

Why dont you use a class instead ?

Code: Select all

<?php
class DbConnector {

var $theQuery;
var $link;

//*** Function: DbConnector, Purpose: Connect to the database ***
function DbConnector(){


// Get the main settings from the array we just loaded
$host = 'dbhost';
$db = 'dbname';
$user = 'dbusername';
$pass = 'dbpassword';

// Connect to the database
$this->link = mysql_connect ($host, $user, $pass)or die ("Unable to connect to Database Server");
mysql_select_db($db)or die ("There is no such database");
register_shutdown_function(array(&$this, 'close'));

}



//*** Function: close, Purpose: Close the connection ***
function close() {

mysql_close($this->link);

}


}
?>
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

You might look into the usage of the mysql_pconnect() function.
It may have advantages to viewer and to server.
Post Reply