我一直在试图找出将我的 PDO 数据库连接包含在我的课程中的最佳方法。这是我到目前为止的代码,但不起作用。
class Delete {
private $connection;
function __construct() {
$this->open_connection();
}
public function open_connection() {
$this->connection = mysql_connect(localhost, 1, 1);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
} else {
$db_select = mysql_select_db(1, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}
public function delete_file($deletelink) {
$dbh = $this->connection;
$sth = $dbh->prepare("SELECT hash FROM files WHERE delete_link = :delete_link");
//PARAM_INT for int, PARAM_STR for string, PARAM_BOOL for bool
$sth->bindParam(':delete_link', $deletedlink, PDO::PARAM_STR);
$sth->execute();
$countrows = $sth->rowCount();
if ($countrows == 0) {
return false;
} else {
$sth = $dbh->prepare("SELECT filename, hash, ext FROM files WHERE delete_link = :delete_link");
//PARAM_INT for int, PARAM_STR for string, PARAM_BOOL for bool
$sth->bindParam(':delete_link', $deletedlink, PDO::PARAM_STR);
$sth->execute();
}
}
如果有人有更好的方法,请分享=)谢谢大家!