可能重复:
关闭 mysql 连接 (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() {
}
}
?>