Update database.php

This commit is contained in:
Namhyeon Go 2018-02-13 17:07:49 +09:00 committed by GitHub
parent ac6cba6e50
commit efb8c0dda7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,86 +1,112 @@
<?php <?php
$conn = new PDO( if(!function_exists("get_db_connect")) {
sprintf( function get_db_connect() {
"mysql:host=%s;dbname=%s;charset=utf8", global $config;
$config['db_host'],
$config['db_name']
),
$config['db_username'],
$config['db_password'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$conn->query("SET NAMES 'utf8'");
if(!function_exists("sql_query")) { $conn = new PDO(
function sql_query($sql, $bind=array()) { sprintf(
return get_db_stmt($sql, $bind); "mysql:host=%s;dbname=%s;charset=utf8",
$config['db_host'],
$config['db_name']
),
$config['db_username'],
$config['db_password'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
$conn->query("SET NAMES 'utf8'");
return $conn;
} }
} }
function get_dbc_object() { if(!function_exists("exec_stmt_query")) {
global $dbc; function exec_stmt_query($sql, $bind=array()) {
return $dbc; $stmt = get_db_stmt($sql, $bind);
$stmt->execute();
return $stmt;
}
} }
function get_db_stmt($sql, $bind=array()) { if(!function_exists("get_dbc_object")) {
$stmt = get_dbc_object()->prepare($sql); function get_dbc_object($renew=false) {
if(count($bind) > 0) { global $dbc;
foreach($bind as $k=>$v) {
$stmt->bindParam(':' . $k, $v); if($renew) {
$dbc = get_db_connect();
} }
return $dbc;
} }
return $stmt;
} }
function get_db_last_id() { if(!function_exists("get_db_stmt")) {
return get_dbc_object()->lastInsertId(); function get_db_stmt($sql, $bind=array()) {
$stmt = get_dbc_object()->prepare($sql);
if(count($bind) > 0) {
foreach($bind as $k=>$v) {
$stmt->bindParam(':' . $k, $v);
}
}
return $stmt;
}
} }
function exec_db_query($sql, $bind=array(), $options=array()) { if(!function_exists("get_db_last_id")) {
$dbc = get_dbc_object(); function get_db_last_id() {
return get_dbc_object()->lastInsertId();
}
}
$flag = false; if(!function_exists("exec_db_query")) {
$stmt = get_db_stmt($sql, $bind); function exec_db_query($sql, $bind=array(), $options=array()) {
$dbc = get_dbc_object();
$validOptions = array(); $flag = false;
$optionAvailables = array("is_check_count", "is_commit"); $stmt = get_db_stmt($sql, $bind);
foreach($optionAvailables as $opt) {
if(!array_key_empty($opt, $options)) { $validOptions = array();
$validOptions[$opt] = $options[$opt]; $optionAvailables = array("is_check_count", "is_commit");
foreach($optionAvailables as $opt) {
if(!array_key_empty($opt, $options)) {
$validOptions[$opt] = $options[$opt];
} else {
$validOptions[$opt] = false;
}
}
extract($validOptions);
if($is_commit) {
$dbc->beginTransaction();
}
if($is_check_count == true) {
if($stmt->execute() && $stmt->rowCount() > 0) {
$flag = true;
}
} else { } else {
$validOptions[$opt] = false; $flag = $stmt->execute();
} }
}
extract($validOptions);
if($is_commit) {
$dbc->beginTransaction();
}
if($is_check_count == true) { if($is_commit) {
if($stmt->execute() && $stmt->rowCount() > 0) { $dbc->commit();
$flag = true;
} }
} else {
$flag = $stmt->execute();
}
if($is_commit) { return $flag;
$dbc->commit();
} }
return $flag;
} }
function exec_db_fetch_all($sql, $bind=array()) { if(!function_exists("exec_db_fetch_all")) {
$rows = array(); function exec_db_fetch_all($sql, $bind=array()) {
$stmt = get_db_stmt($sql, $bind); $rows = array();
$stmt = get_db_stmt($sql, $bind);
if($stmt->execute() && $stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); if($stmt->execute() && $stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
return $rows;
} }
return $rows;
} }
// set global db connection variable // set global db connection variable