Update database.php

This commit is contained in:
Namhyeon Go 2020-01-09 19:45:01 +09:00 committed by GitHub
parent 4b5e30dfc9
commit 6d094abaae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
/** /**
* @file database.php * @file database.php
* @created_on 2018-04-13 * @created_on 2018-04-13
* @updated_on 2020-01-08 * @updated_on 2020-01-09
* @author Go Namhyeon <gnh1201@gmail.com> * @author Go Namhyeon <gnh1201@gmail.com>
* @brief Database module * @brief Database module
*/ */
@ -21,34 +21,33 @@ if(!check_function_exists("check_db_driver")) {
} }
if(!check_function_exists("get_db_connect")) { if(!check_function_exists("get_db_connect")) {
function get_db_connect($a=3, $b=0) { function get_db_connect($_RETRY=0) {
$conn = false; $conn = false;
$config = get_config(); $config = get_config();
$db_driver = get_db_driver(); $db_driver = get_db_driver();
$dsn = "mysql:host=%s;dbname=%s;charset=utf8";
$_LIMIT_RETRY = get_value_in_array("db_retry_limit", $config, 3);
if(in_array($db_driver, array("mysql", "mysql.pdo"))) { if(in_array($db_driver, array("mysql", "mysql.pdo"))) {
try { try {
$conn = new PDO( $conn = new PDO(
sprintf( sprintf($dsn, $config['db_host'], $config['db_name']),
"mysql:host=%s;dbname=%s;charset=utf8",
$config['db_host'],
$config['db_name']
),
$config['db_username'], $config['db_username'],
$config['db_password'], $config['db_password'],
array( array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
) )
); );
//$conn->query("SET NAMES 'utf8'"); //$conn->query("SET NAMES utf8");
} catch(Exception $e) { } catch(Exception $e) {
if($b > $a) { if($_RETRY > $_LIMIT_RETRY) {
set_error($e->getMessage()); set_error($e->getMessage());
show_errors(); show_errors();
} else { } else {
$b++; $_RETRY++;
sleep(0.03); $conn = get_db_connect($_RETRY);
$conn = get_db_connect($a, $b);
} }
} }
} elseif(loadHelper("database.alt")) { } elseif(loadHelper("database.alt")) {