douqie3391 2012-06-19 08:32
浏览 16
已采纳

我什么时候应该在班上关闭一个开放的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() {



    }

    }
?>
  • 写回答

3条回答 默认 最新

  • dongruo4601 2012-06-19 08:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP