我什么时候应该在班上关闭一个开放的MySQL连接? [重复]

Possible Duplicate:
Close mysql connection (PHP)

Please help me, I not know where add mysql_close($db) ;

See my code:

<?php 

    class atsilipimai {

    private $db;

    public function __construct($host, $user, $password, $db) {

    $this->db = mysql_connect($host, $user, $password) or die('Couldn\'t connect to mysql');
    mysql_select_db($db) or die('Couldn\'t connect to db');
    }

    public function  addPost() {
    if ( isset($_POST['submit']) ) {
            $this->name = mysql_real_escape_string(addslashes($_POST['name']));
            $this->msg = mysql_real_escape_string(addslashes($_POST['msg']));
            $this->date = date("Y-m-d H:i:s");



            if (empty($this->name) || empty($this->msg)) {
                echo "<p>Please enter all details!</p>";
            } else {
                $sql = "INSERT INTO atsiliepimai (name, msg, date)";
                $sql .= " VALUES ('$this->name', '$this->msg', '$this->date')";
                mysql_query($sql) or die(mysql_error());

            }
        }
    }

    public function showPost() {
        //I'm not adding pagination, so it will only show last 5 posts.
        $sql = 'SELECT * FROM atsiliepimai ORDER BY id DESC LIMIT 5';
        $query = mysql_query($sql) or die('Couldn\'t get posts from database');
        while ( $row = mysql_fetch_array($query) ) {
            $this->name = htmlentities(stripslashes($row['name']));
            $this->msg = htmlentities(stripslashes($row['msg']));
            $this->date = htmlentities(stripslashes($row['date']));


            //Just add div's or what you want for the output.
            //Ip wont be added.
            echo "
            <h4>".$this->name."</h4>
            <p>".$this->msg."</p>
            <p class = 'data' >".$this->date."</p>
            <hr />
            ";

        }

    }

    public function __destruct() {



    }

    }
?>

From manual mysql_close

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

So you do not need to close it, unless you have some special cases you need to terminate connection then put this function.

Also please use mysqli_* or PDO for interaction with database as mysql_* function deprecation preocess started

Using a class is not OOP. You have no separation of concerns. You have to encapsulate the database access in own class, and visualisation should be separated as well. The closing of DB connection handler can be implemented in the destructor of the DB class. But in your solution, each object, that works with the DB, will have it's own connection handler, and will be resposible for rendering as well, so right now your class is just a namespace...

you shouldn't make connections to your database here. You should create a database class that handles all communication with your database, opening (in _construct()) and closing (in _destruct()) connections should be done there. You just simply create and use an object of the said class in any other class that requires database utilities.