<?php
class DBDataClass {
protected $db; // 변수를 선언한 클래스와 상속받은 클래스에서 참조할 수 있다.
public function __construct() {
$this->dbConnect();
// construct 메소드는 객체가 생성(인스턴스화)될 때 자동으로 실행되는 특수한 메소드다.
}
private function dbConnect() {
require_once 'dbinfo.php';
try {
// SQL PDO 객체 생성
$this->db = new PDO(_DSN, _DBUSER, _DBPASS);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
} catch(PDOException $ex) {
die("오류 : " . $ex->getMessage());
}
}
// 신규 자료 추가(ok)
function getDbInsert($table, $key, $val) {
try {
$params = explode(',', $val); // 문자열을 분리하여 배열로 만듬
$this->db->beginTransaction();
$sql = "insert into " . $table . " (" . $key . ") values(" . $this->recur_quot(count($params)) . ")";
$stmt = $this->db->prepare($sql);
$status = $stmt->execute($params); // $params 는 배열 값
$this->db->commit();
return 1;
} catch (PDOException $pex) {
$this->db->rollBack();
echo "에러 : " . $pex->getMessage();
return 0;
}
}
public function recur_quot($cnt) {
$R = array();
for ($i = 0; $i < $cnt; $i++) {
array_push($R, "?");
}
return implode(",", $R); // 배열을 문자열로
}
function getDbUpdate($table, $set, $params, $where) {
$sql = "update " . $table . " set " . $set . ($where ? ' where ' . $where : '');
try {
$this->db->beginTransaction();
$stmt = $this->db->prepare($sql);
$status = $stmt->execute($params);
$this->db->commit();
return 1;
} catch (PDOException $pex) {
$this->db->rollBack();
echo "에러 : " . $pex->getMessage();
return 0;
}
}
public function getDbDelete($table, $where, $params) {
if ($this->isDataExisted($table, $where, $params)) {
try {
$this->db->beginTransaction();
$sql = "delete from " . $table . ($where ? ' where ' . $where : '');
$stmt = $this->db->prepare($sql);
$status = $stmt->execute($params);
$this->db->commit();
return 1; // 삭제 성공
} catch (PDOException $pex) {
$this->db->rollBack();
echo "에러 : " . $pex->getMessage();
}
} else {
return 0; // 삭제할 데이터가 없음
}
}
// 삭제할 데이터의 존재 유무 파악
public function isDataExisted($table, $where, $params) {
$sql = 'select * from ' . $table . ($where ? ' where ' . $where : '');
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
if ($row = $stmt->fetch()) {
return $row;
} else {
return false;
}
}
// 검색조건에 일치하는 데이터 가져오기
public function getDbData($table, $where, $column, $returntype = '') {
$sql = 'select ' . $column . ' from ' . $table . ($where ? ' where ' . $where : '');
$stmt = $this->db->prepare($sql);
$stmt->execute();
if ($returntype == 1) {
return $stmt->fetch(PDO::FETCH_ASSOC);
} else {
return $stmt->fetch();
}
}
// 검색조건에 일치하는 데이터 가져오기
public function getDbDataAll($table, $where, $column) {
$sql = 'select ' . $column . ' from ' . $table . ($where ? ' where ' . $where : '');
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
// DB Query result 함수
function getDbresult($table,$where,$column) {
$sql = 'select ' . $column . ' from ' . $table . ($where ? ' where ' .$where : '');
$stmt = $this->db->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
// DB 레코드 총 수(ok)
public function getDbRows($table, $where) {
$sql = 'select count(*) from ' . $table . ($where ? ' where ' . $where : '');
$stmt = $this->db->prepare($sql);
$stmt->execute();
if ($row = $stmt->fetch()) {
return $row[0];
} else {
return false;
}
}
}
?>