평소 작업을 하기 위해서 SQL 인젝션도 생각해야 되고 조금 귀찮은 부분이 있었는데….
이제는 그냥 PDO를 사용해 버립니다.
그럼으로 인해서 인젝션는 그냥 조금 무시하는 편입니다.
<?php
class dbMysql {
private $host = "localhost";
private $user = "사용자";
private $pass = "암호";
private $dbname = "데이터베이스";
private $dbh;
private $error;
private $stmt;
public function __construct() {
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
$this->dbh->exec("SET CHARACTER SET utf8");
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
/**
* @brief
*
**/
public function query($query){
$this->stmt = $this->dbh->prepare($query);
//echo $query.chr(10);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
/**
* @brief
*
**/
public function execute(){
return $this->stmt->execute();
}
/**
* @brief
*
**/
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
/**
* @brief
*
**/
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
/**
* @brief
*
**/
public function rowCount(){
return $this->stmt->rowCount();
}
/**
* @brief
*
**/
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
/**
* @brief
*
**/
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
/**
* @brief
*
**/
public function showMessage() {
echo $this->error;
}
/**
* @brief
*
**/
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
/**
* @brief
*
**/
public function endTransaction(){
return $this->dbh->commit();
}
/**
* @brief
*
**/
public function cancelTransaction(){
return $this->dbh->rollBack();
}
}