나는 내 클래스에 내 PDO 데이터베이스 연결을 포함시키는 가장 좋은 방법을 찾아 내려고 노력했습니다. 여기에 내가 지금까지 가지고 있고 작동하지 않을 코드가 있습니다.클래스에 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();
}
}
사람이 그 일을 더 나은 방법이있는 경우 = 공유하시기 바랍니다) 고마워!
당신은 당신이 사용하고있는 모든 클래스에서 PDO 연결을 만들 건가요?! –
고대의'mysql_ *'함수로 새로운 코드 작성을 중단하십시오. 그들은 더 이상 유지되지 않으며 커뮤니티는 [비추천 프로세스]를 시작했습니다 (http://news.php.net/php.internals/53799). 대신 준비된 구문을 배우고 [PDO] (http://php.net/pdo) 또는 [MySQLi] (http://php.net/mysqli)를 사용해야합니다. 만약 당신이 배울 점이 있다면, 여기 [PDO 관련 튜토리얼] (http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). –
이러한 쿼리는 PDO – HarryBeasant