Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. <?php
  2.  
  3. class config
  4. {
  5. /**
  6. * @var string $config_file
  7. */
  8.  
  9.  
  10. /**
  11. * @var array $config_values;
  12. */
  13. public $config_values = array('db_local'=>array(
  14. 'dbname'=>'base',
  15. 'host'=>'localhost',
  16. 'user'=>'root',
  17. 'password'=>''
  18. ));
  19.  
  20. /**
  21. * @var object $instance
  22. */
  23. private static $instance = null;
  24.  
  25. /**
  26. * @the constructor is set to private so
  27. * @so nobody can create a new instance using new
  28. */
  29. private function __construct()
  30. {
  31.  
  32. }
  33.  
  34. /**
  35. * Return Config instance or create intitial instance
  36. * @access public
  37. * @return object
  38. */
  39. public static function getInstance()
  40. {
  41. if(is_null(self::$instance))
  42. {
  43. self::$instance = new config();
  44. }
  45. return self::$instance;
  46. }
  47.  
  48. /**
  49. * @get a config option by key
  50. * @access public
  51. * @param string $key:The configuration setting key
  52. * @return string
  53. */
  54. public function getValue($key)
  55. {
  56. return self::$config_values[$key];
  57. }
  58.  
  59. /**
  60. * @__clone
  61. * @access private
  62. */
  63. private function __clone()
  64. {
  65. ;
  66. }
  67. }
  68. // Site configuration
  69. class DB {
  70. /**
  71. * @var DB
  72. */
  73. protected static $instance = null;
  74.  
  75. private $result;
  76. private $query;
  77.  
  78. private $_fetch_row = false;
  79.  
  80. public $debug = 0;
  81. public $queries = array();
  82.  
  83.  
  84. private function __construct() {
  85. self::connect();
  86.  
  87. if (isset($_REQUEST['showqueries']) && $_REQUEST['showqueries']) {
  88. $this->debug = 1;
  89. }
  90. }
  91. /**
  92. * Returns instance of DB
  93. * @return DB
  94. */
  95. public static function getInstance() {
  96. if (is_null(self::$instance))
  97. {
  98. self::$instance = new DB();
  99. }
  100. return self::$instance;
  101. }
  102.  
  103. private function connect() {
  104. $config = config::getInstance()->config_values;
  105. mysql_select_db(
  106. $config['db_local']['base'],
  107. mysql_connect(
  108. $config['db_local']['localhost'],
  109. $config['db_local']['root'],
  110. $config['db_local']['']
  111. )
  112. );
  113. }
  114.  
  115. public function query($query) {
  116. $this->result = array();
  117. $this->query = trim($query);
  118.  
  119. if ($this->debug) {
  120. $time_start = microtime(true);
  121. }
  122.  
  123. $result = mysql_query($this->query);
  124.  
  125. if ($this->debug) {
  126. $time_run = microtime(true) - $time_start;
  127. $this->queries[] = array('query' => $this->query, 'time' => $time_run);
  128. }
  129.  
  130. if (!$result) {
  131. throw new Exception($this->error());
  132. }
  133.  
  134. if ($this->_fetch_row)
  135. {
  136. $this->result = mysql_fetch_row($result);
  137. }
  138. elseif (preg_match('/^select /i', $this->query) || preg_match('/^show /i', $this->query))
  139. {
  140. while ($this->result[] = mysql_fetch_assoc($result)) {}
  141. }
  142.  
  143. return $result;
  144. }
  145.  
  146. public function fetch_all($query = '') {
  147. if ($query)
  148. {
  149. $this->query($query);
  150. }
  151. return $this->result;
  152. }
  153.  
  154. public function fetch_row($query = '') {
  155. if ($query)
  156. {
  157. $this->query($query);
  158. }
  159. return $this->result[0];
  160. }
  161.  
  162. public function fetch_val($query = '') {
  163. if ($query)
  164. {
  165. $this->_fetch_row = true;
  166. $this->query($query);
  167. $this->_fetch_row = false;
  168. }
  169. return $this->result[0];
  170. }
  171.  
  172. public function result($index, $field) {
  173. if ($index < 0 || $index >= sizeof($this->result)) {
  174. throw new Exception('DB error: ' . $index . ': no such index');
  175. }
  176. return $this->result[$index][$field];
  177. }
  178.  
  179. public function numrows() {
  180. return sizeof($this->result);
  181. }
  182.  
  183. public function error() {
  184. return mysql_error();
  185. }
  186.  
  187. public function insert_id() {
  188. return mysql_insert_id();
  189. }
  190.  
  191. /**
  192. * Singleton
  193. */
  194. protected function __clone() {
  195. ;
  196. }
  197. }
  198.  
  199. $db = DB::getInstance();
  200.  
  201. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement