{"id":1008,"date":"2004-01-16T09:11:26","date_gmt":"2004-01-16T09:11:26","guid":{"rendered":"http:\/\/dev.jblove.net\/?p=1008"},"modified":"2004-01-16T09:11:26","modified_gmt":"2004-01-16T09:11:26","slug":"mysql-db-class","status":"publish","type":"post","link":"https:\/\/jblove.net\/?p=1008","title":{"rendered":"mysql DB Class"},"content":{"rendered":"<p><?php<br \/>\n\/* vim: set expandtab tabstop=4 shiftwidth=4: *\/<br \/>\n\/\/ +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n\/\/ | PHP version 4.3.x                                      |<br \/>\n\/\/ +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n\/\/ | Copyright (c) 2003 Song Hyo-Jin                        |<br \/>\n\/\/ +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n\/\/ | This source file is GPL.                               |<br \/>\n\/\/ +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n\/\/ | Author : Song Hyo-Jin <crosser at hanmail dot net>     |<br \/>\n\/\/ |                                  (MSN Messengerable)   |<br \/>\n\/\/ +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n\/\/<br \/>\n\/\/ $Id: mysql.inc, v 0.01 2004\/01\/15 11:40:00 crosser Exp $<br \/>\n\/\/<br \/>\n\/\/ MySQL database control<\/p>\n<p>\/*<br \/>\n$test = new MyDB(&#8216;access_file.ini&#8217;, [transaction bool]);<br \/>\n$test->query(&#8216;sql query&#8217;);<br \/>\n$test->close();<br \/>\n$data = mysql_fetch_object($test->result);<\/p>\n<p>Want query error report<br \/>\n$test->err_report = true;<br \/>\n$test->query(&#8216;sql query&#8217;);<\/p>\n<p>With transaction<br \/>\n$test = new MyDB(&#8216;access_file.ini&#8217;, true);<br \/>\n$test->query(&#8216;sql query&#8217;);<br \/>\n$test->query(&#8216;sql query&#8217;);<br \/>\n$test->query(&#8216;sql query&#8217;);<br \/>\nif(not good)<br \/>\n    $test->close_error(&#8216;error message&#8217;);<br \/>\n\/\/ or exit(&#8216;error message&#8217;); auto rollback;<\/p>\n<p>access_file.ini<\/p>\n<p>ip = localhost<br \/>\nuser = sql_user<br \/>\npass = sql_pass<br \/>\ndb = sql_db<br \/>\n*\/<\/p>\n<p>\/\/ MySQL Unsigned Limit<br \/>\ndefine(&#8216;MyBIGINT&#8217;, &#8216;18446744073709551615&#8217;);<br \/>\ndefine(&#8216;MyINT&#8217;, 4294967295);<br \/>\ndefine(&#8216;MyMEDIUMINT&#8217;, 16777215);<br \/>\ndefine(&#8216;MySMALLINT&#8217;, 65535);<br \/>\ndefine(&#8216;MyTINYINT&#8217;, 255);<\/p>\n<p>$db_set_file = array();<\/p>\n<p>class MyDB<br \/>\n{<br \/>\n    var $conn, $transaction, $result, $err_report, $query_count;<\/p>\n<p>    function MyDB($db_setting, $transaction = false)<br \/>\n    {<br \/>\n        $this->err_report = false;<br \/>\n        $this->transaction = $transaction;<br \/>\n        $this->query_count = 0;<br \/>\n        static $db_set_file = array();<\/p>\n<p>        if(false === array_key_exists($db_setting, $db_set_file))<br \/>\n            $db_set_file[$db_setting] = parse_ini_file($db_setting);<br \/>\n        $myset = &#038;$db_set_file[$db_setting];<br \/>\n        $this->conn = mysql_connect($myset[&#8216;ip&#8217;], $myset[&#8216;user&#8217;], $myset[&#8216;pass&#8217;]) or exit(&#8216;mysql_connect error&#8217;);<br \/>\n        mysql_select_db($myset[&#8216;db&#8217;], $this->conn) or exit(&#8216;mysql_select_db error&#8217;);<br \/>\n        if($transaction) {<br \/>\n            mysql_query(&#8216;SET AUTOCOMMIT=0&#8217;, $this->conn) or exit(&#8216;mysql_transaction_autocommit error&#8217;);<br \/>\n            mysql_query(&#8216;BEGIN&#8217;, $this->conn) or exit(&#8216;mysql_transaction_begin error&#8217;);<br \/>\n            register_shutdown_function(array(&#038;$this, &#8216;close_error&#8217;));<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    function close()<br \/>\n    {<br \/>\n        if($this->transaction)<br \/>\n            mysql_query(&#8216;COMMIT&#8217;, $this->conn) or exit(&#8216;mysql_transaction_commit error&#8217;);<br \/>\n        mysql_close($this->conn) or exit(&#8216;mysql_close error&#8217;);<br \/>\n        $this->conn = false;<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    function close_error($message = &#8216;mysql_error exit&#8217;)<br \/>\n    {<br \/>\n        if($this->transaction)<br \/>\n            mysql_query(&#8216;ROLLBACK&#8217;, $this->conn) or exit(&#8216;mysql_transaction_rollback error&#8217;);<br \/>\n        mysql_close($this->conn) or exit(&#8216;mysql_close_error error&#8217;);<br \/>\n        exit($message);<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    function query($query)<br \/>\n    {<br \/>\n        $this->query_count ++;<br \/>\n        $this->result = mysql_query($query, $this->conn) or $this->close_error($this->err_report?&#8217;query < '.$query.' > error (count : &#8216;.$this->query_count.&#8217; ) : &#8216;.mysql_error($this->conn):&#8217;query error (count : &#8216;.$this->query_count.&#8217; )&#8217;);<br \/>\n        return true;<br \/>\n    }<br \/>\n}<\/p>\n<p>?> <\/p>\n<p>\n\uc0ac\uc6a9\ubc95<\/p>\n<p>\uc5f0\uacb0<br \/>\n$test = new MyDB(&#8216;access_file.ini&#8217;);<\/p>\n<p>\ud2b8\ub79c\uc7ad\uc158\uc6a9 \uc5f0\uacb0<br \/>\n$test = new MyDB(&#8216;access_file.ini&#8217;, true);<\/p>\n<p>\ucffc\ub9ac<br \/>\n$test->query(&#8216;sql query&#8217;);<\/p>\n<p>\uc885\ub8cc (\ud2b8\ub79c\uc7ad\uc158\uc2dc \uc790\ub3d9 COMMIT)<br \/>\n$test->close();<\/p>\n<p>\ubc1b\uc544\uc628\uac12<br \/>\n$data = mysql_fetch_object($test->result);<\/p>\n<p>\uc624\ub958\ucd9c\ub825 \uc6d0\ud560\uc2dc<br \/>\n$test->err_report = true;<br \/>\n$test->query(&#8216;error query&#8217;);<\/p>\n<p>\uc624\ub958\uc788\uc744\uc2dc\uc5d0 \uc885\ub8cc (\ud2b8\ub79c\uc7ad\uc158\uc2dc \uc790\ub3d9 ROLLBACK)<br \/>\n$test->close_error(&#8216;error message&#8217;);<\/p>\n<p>exit() \ub9cc \ud574\ub3c4 \ubb34\uc870\uac74 ROLLBACK (\uc544\ub9c8 $test->close() \uc548\ud558\uace0 \uc885\ub8cc\ub418\ub3c4 ROLLBACK)<\/p>\n<p>mysql_insert_id \ub4f1\uc758 \ud568\uc218 \uc0ac\uc6a9\uc2dc\uc5d0 $test->conn \ub9c1\ud06c \uc0ac\uc6a9.<br \/>\n$idx = mysql_insert_id($test->conn);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc0ac\uc6a9\ubc95 \uc5f0\uacb0 $test = new MyDB(&#8216;access_file.ini&#8217;); \ud2b8\ub79c\uc7ad\uc158\uc6a9 \uc5f0\uacb0 $test = new MyDB(&#8216;access_file.ini&#8217;, true); \ucffc\ub9ac $test->query(&#8216;sql query&#8217;); \uc885\ub8cc (\ud2b8\ub79c\uc7ad\uc158\uc2dc \uc790\ub3d9 COMMIT) $test->close(); \ubc1b\uc544\uc628\uac12 $data = mysql_fetch_object($test->result); \uc624\ub958\ucd9c\ub825 \uc6d0\ud560\uc2dc $test->err_report = true; $test->query(&#8216;error query&#8217;); \uc624\ub958\uc788\uc744\uc2dc\uc5d0 \uc885\ub8cc (\ud2b8\ub79c\uc7ad\uc158\uc2dc \uc790\ub3d9 ROLLBACK) $test->close_error(&#8216;error message&#8217;); exit() \ub9cc \ud574\ub3c4 \ubb34\uc870\uac74 ROLLBACK (\uc544\ub9c8 $test->close() \uc548\ud558\uace0 \uc885\ub8cc\ub418\ub3c4 ROLLBACK) mysql_insert_id \ub4f1\uc758 \ud568\uc218 \uc0ac\uc6a9\uc2dc\uc5d0 $test->conn \ub9c1\ud06c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[51],"tags":[],"class_list":["post-1008","post","type-post","status-publish","format-standard","hentry","category-papa-php"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/posts\/1008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1008"}],"version-history":[{"count":0,"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/posts\/1008\/revisions"}],"wp:attachment":[{"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}