/lib/php/class.db.php

https://github.com/pixelephant/mak-web · PHP · 370 lines · 253 code · 115 blank · 2 comment · 50 complexity · 79683281977fb12a12174d9021934280 MD5 · raw file

  1. <?php
  2. class db{
  3. protected $dbhost = 'localhost';
  4. protected $dbuser = 'mak';
  5. protected $dbpass = '8Xs7@D7d3#83qzBR';
  6. protected $db = 'mak';
  7. public $lastInsert;
  8. private $error_db = 'DB error';
  9. private $error_param = 'Parameter error';
  10. protected $debug = false;
  11. private $sqlQuery;
  12. public function __construct($dbhost,$dbuser,$dbpass,$db,$names='utf8',$debug=false){
  13. if($dbhost != ''){
  14. $this->dbhost = $dbhost;
  15. }
  16. if($dbuser != ''){
  17. $this->dbuser = $dbuser;
  18. }
  19. if($dbpass != ''){
  20. $this->dbpass = $dbpass;
  21. }
  22. if($db != ''){
  23. $this->db = $db;
  24. }
  25. if($names == ''){
  26. $names = 'utf8';
  27. }
  28. $this->debug = $debug;
  29. $this->mysql = mysqli_init();
  30. if(!$this->mysql->real_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->db)){
  31. return FALSE;
  32. }
  33. $this->query("SET NAMES '".$names."'");
  34. }
  35. public function query($sql){
  36. $this->sqlQuery = $this->mysql->query($sql);
  37. $this->insertId();
  38. if($this->debug){
  39. echo __CLASS__ . " -> " . __FUNCTION__ . " : " . $sql . "<br />";
  40. }
  41. if($this->sqlQuery){
  42. return $this->sqlQuery;
  43. }else{
  44. //return FALSE;
  45. return $this->getSqlError();
  46. }
  47. }
  48. public function results($query='',$params){
  49. if(!is_array($params)){
  50. //return $this->error_param;
  51. $params = explode(",",$params);
  52. }
  53. $results = array();
  54. $results['count'] = 0;
  55. if($query != ''){
  56. $this->sqlQuery = $query;
  57. }
  58. foreach($params as $k => $v){
  59. if(strpos($v," AS ") !== false){
  60. $pos = strpos($v," AS ")+ 4 ;
  61. $params[$k] = substr($v,$pos);
  62. }
  63. }
  64. while ($row = $this->sqlQuery->fetch_object()) {
  65. foreach($params as $k){
  66. $results[$results['count']][$k] = $row->$k;
  67. }
  68. $results['count']++;
  69. }
  70. return $results;
  71. }
  72. protected function insertId(){
  73. $this->lastInsert = $this->mysql->insert_id;
  74. }
  75. protected function num_rows(){
  76. return $this->sqlQuery->num_rows;
  77. }
  78. public function getInsertId(){
  79. return $this->lastInsert;
  80. }
  81. protected function success(){
  82. if($this->mysql->affected_rows>0){
  83. return TRUE;
  84. }else{
  85. return FALSE;
  86. }
  87. }
  88. public function real_escape_string($string){
  89. return $this->mysql->real_escape_string($string);
  90. }
  91. protected function select($table,$col,$cond='',$join=''){
  92. $col = str_replace("\n","",$col);
  93. $col = explode(",",$col);
  94. if(!is_array($cond)){
  95. $cond = array($cond);
  96. }
  97. if(!is_array($join) && $join != ''){
  98. return false;
  99. }
  100. if(isset($cond['limit'])){
  101. $limit = $cond['limit'];
  102. unset($cond['limit']);
  103. }
  104. if(isset($cond['orderby'])){
  105. $order = $cond['orderby'];
  106. unset($cond['orderby']);
  107. }
  108. $sql = "SELECT DISTINCT ";
  109. foreach($col as $key => $val){
  110. $sql .= $this->mysql->real_escape_string($val).',';
  111. }
  112. $sql = substr($sql,0,-1);
  113. $sql .= " FROM ".$table;
  114. if($join != ''){
  115. foreach($join as $key => $value){
  116. if(!isset($join[$key]['type'])){
  117. $join[$key]['type'] = 'INNER JOIN';
  118. }
  119. $sql .= " ".$join[$key]['type'];
  120. $sql .= " ".$join[$key]['table'];
  121. $sql .= " ON ".$join[$key]['value'];
  122. }
  123. }
  124. $c = '';
  125. if($cond != ''){
  126. foreach($cond as $key => $val){
  127. if(is_array($cond[$key])){
  128. $c .= ' '.(isset($cond[$key]['and_or']) ? $cond[$key]['and_or'] : 'AND').' '.$key." ".$cond[$key]['rel']." '".$cond[$key]['val']."'";
  129. }else{
  130. if($val != ''){
  131. $c .= " AND ".$key."='".$val."'";
  132. }
  133. }
  134. }
  135. }
  136. $rep = 0;
  137. $c = preg_replace('/ AND /',' WHERE ',$c,1,$rep);
  138. if($rep == 0){
  139. $c = preg_replace('/ OR /',' WHERE ',$c,1);
  140. }
  141. $sql .= $c;
  142. if(isset($order)){
  143. $sql .= " ORDER BY ".$order;
  144. }
  145. if(isset($limit)){
  146. $sql .= " LIMIT ".(int)$limit;
  147. }
  148. if($this->debug){
  149. echo __CLASS__ . " -> " . __FUNCTION__ . " : " . $sql . "<br />";
  150. }
  151. return $sql;
  152. }
  153. protected function insert($table,$col_val){
  154. if(!is_array($col_val)){
  155. return FALSE;
  156. }
  157. $sql = "INSERT INTO ";
  158. $sql .= $table;
  159. $c = " (";
  160. $v = "(";
  161. foreach($col_val as $key => $val){
  162. $c .= $this->mysql->real_escape_string($key).',';
  163. $v .= "'".$this->mysql->real_escape_string($val)."',";
  164. }
  165. $c = substr($c,0,-1).")";
  166. $v = substr($v,0,-1).")";
  167. $sql = $sql . $c . " VALUES " . $v;
  168. if($this->debug){
  169. echo __CLASS__ . " -> " . __FUNCTION__ . " : " . $sql . "<br />";
  170. }
  171. return $sql;
  172. }
  173. protected function update($table,$col_val,$cond=''){
  174. if(!is_array($col_val) || (!is_array($cond) && $cond != '')){
  175. return FALSE;
  176. }
  177. $sql = "UPDATE ";
  178. $sql .= $table;
  179. $sql .= " SET ";
  180. foreach($col_val as $key => $val){
  181. $sql .= $this->mysql->real_escape_string($key)."='".$this->mysql->real_escape_string($val)."',";
  182. }
  183. $sql = substr($sql,0,-1);
  184. if($cond != ''){
  185. foreach($cond as $k => $v){
  186. $sql .= " AND ".$k."='".$v."'";
  187. }
  188. }
  189. $sql = preg_replace('/ AND /',' WHERE ',$sql,1);
  190. if($this->debug){
  191. echo __CLASS__ . " -> " . __FUNCTION__ . " : " . $sql . "<br />";
  192. }
  193. return $sql;
  194. }
  195. protected function delete($table,$cond=''){
  196. $sql = "DELETE FROM " . $table;
  197. if($cond != ''){
  198. foreach($cond as $k => $v){
  199. $sql .= " AND ".$k."='".$v."'";
  200. }
  201. }
  202. $sql = preg_replace('/ AND /',' WHERE ',$sql,1);
  203. if($this->debug){
  204. echo __CLASS__ . " -> " . __FUNCTION__ . " : " . $sql . "<br />";
  205. }
  206. return $sql;
  207. }
  208. protected function commit(){
  209. $this->mysql->query('COMMIT');
  210. }
  211. protected function rollback(){
  212. $this->mysql->query('ROLLBACK');
  213. }
  214. protected function begin(){
  215. $this->mysql->query('BEGIN');
  216. }
  217. public function close(){
  218. $this->mysql->close();
  219. }
  220. public function normalizeString($string){
  221. $before = array(" ","ö","ü","ó","ő","ú","ű","á","í","é","ő","ű",":","%","/","(",")");
  222. $after = array("_","o","u","o","o","u","u","a","i","e","o","u","","","","","");
  223. $string = trim($string);
  224. $string = str_replace($before,$after,strtolower($string));
  225. return $string;
  226. }
  227. public function getSqlError(){
  228. return $this->mysql->errno;
  229. }
  230. public function sql_select($table,$col,$cond='',$join=''){
  231. $sql = $this->select($table,$col,$cond,$join);
  232. $q = $this->query($sql);
  233. $a = $this->results($q,$col);
  234. return $a;
  235. }
  236. public function sql_insert($table,$col_val){
  237. $sql = $this->insert($table,$col_val);
  238. $q = $this->query($sql);
  239. $a = $this->success();
  240. return $a;
  241. }
  242. public function sql_update($table,$col_val,$cond=''){
  243. $sql = $this->update($table,$col_val,$cond);
  244. $q = $this->query($sql);
  245. $a = $this->success();
  246. return $a;
  247. }
  248. public function sql_delete($table,$cond=''){
  249. $sql = $this->delete($table,$cond);
  250. $q = $this->query($sql);
  251. $a = $this->success();
  252. return $a;
  253. }
  254. }
  255. ?>