gpt4 book ai didi

php - 我应该什么时候关闭类(class)中打开的 MySQL 连接?

转载 作者:行者123 更新时间:2023-12-01 00:55:42 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Close mysql connection (PHP)

请帮帮我,我不知道在哪里添加 mysql_close($db) ;

查看我的代码:

<?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() {



}

}
?>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com