/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +——————————————————–+
// | PHP version 4.3.x |
// +——————————————————–+
// | Copyright (c) 2003 Song Hyo-Jin |
// +——————————————————–+
// | This source file is GPL. |
// +——————————————————–+
// | Author : Song Hyo-Jin
// | (MSN Messengerable) |
// +——————————————————–+
//
// $Id: mysql.inc, v 0.01 2004/01/15 11:40:00 crosser Exp $
//
// MySQL database control
/*
$test = new MyDB(‘access_file.ini’, [transaction bool]);
$test->query(‘sql query’);
$test->close();
$data = mysql_fetch_object($test->result);
Want query error report
$test->err_report = true;
$test->query(‘sql query’);
With transaction
$test = new MyDB(‘access_file.ini’, true);
$test->query(‘sql query’);
$test->query(‘sql query’);
$test->query(‘sql query’);
if(not good)
$test->close_error(‘error message’);
// or exit(‘error message’); auto rollback;
access_file.ini
ip = localhost
user = sql_user
pass = sql_pass
db = sql_db
*/
// MySQL Unsigned Limit
define(‘MyBIGINT’, ‘18446744073709551615’);
define(‘MyINT’, 4294967295);
define(‘MyMEDIUMINT’, 16777215);
define(‘MySMALLINT’, 65535);
define(‘MyTINYINT’, 255);
$db_set_file = array();
class MyDB
{
var $conn, $transaction, $result, $err_report, $query_count;
function MyDB($db_setting, $transaction = false)
{
$this->err_report = false;
$this->transaction = $transaction;
$this->query_count = 0;
static $db_set_file = array();
if(false === array_key_exists($db_setting, $db_set_file))
$db_set_file[$db_setting] = parse_ini_file($db_setting);
$myset = &$db_set_file[$db_setting];
$this->conn = mysql_connect($myset[‘ip’], $myset[‘user’], $myset[‘pass’]) or exit(‘mysql_connect error’);
mysql_select_db($myset[‘db’], $this->conn) or exit(‘mysql_select_db error’);
if($transaction) {
mysql_query(‘SET AUTOCOMMIT=0’, $this->conn) or exit(‘mysql_transaction_autocommit error’);
mysql_query(‘BEGIN’, $this->conn) or exit(‘mysql_transaction_begin error’);
register_shutdown_function(array(&$this, ‘close_error’));
}
return true;
}
function close()
{
if($this->transaction)
mysql_query(‘COMMIT’, $this->conn) or exit(‘mysql_transaction_commit error’);
mysql_close($this->conn) or exit(‘mysql_close error’);
$this->conn = false;
return true;
}
function close_error($message = ‘mysql_error exit’)
{
if($this->transaction)
mysql_query(‘ROLLBACK’, $this->conn) or exit(‘mysql_transaction_rollback error’);
mysql_close($this->conn) or exit(‘mysql_close_error error’);
exit($message);
return true;
}
function query($query)
{
$this->query_count ++;
$this->result = mysql_query($query, $this->conn) or $this->close_error($this->err_report?’query < '.$query.' > error (count : ‘.$this->query_count.’ ) : ‘.mysql_error($this->conn):’query error (count : ‘.$this->query_count.’ )’);
return true;
}
}
?>
사용법
연결
$test = new MyDB(‘access_file.ini’);
트랜잭션용 연결
$test = new MyDB(‘access_file.ini’, true);
쿼리
$test->query(‘sql query’);
종료 (트랜잭션시 자동 COMMIT)
$test->close();
받아온값
$data = mysql_fetch_object($test->result);
오류출력 원할시
$test->err_report = true;
$test->query(‘error query’);
오류있을시에 종료 (트랜잭션시 자동 ROLLBACK)
$test->close_error(‘error message’);
exit() 만 해도 무조건 ROLLBACK (아마 $test->close() 안하고 종료되도 ROLLBACK)
mysql_insert_id 등의 함수 사용시에 $test->conn 링크 사용.
$idx = mysql_insert_id($test->conn);