Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. <?php
  2.  
  3. class Mysql{
  4.  
  5. static private $link = null;
  6. static private $info = array(
  7. 'last_query' => null,
  8. 'num_rows' => null,
  9. 'insert_id' => null
  10. );
  11. static private $connection_info = array();
  12.  
  13. static private $where;
  14. static private $limit;
  15. static private $order;
  16.  
  17. function __construct($host, $user, $pass, $db){
  18. self::$connection_info = array('host' => $host, 'user' => $user, 'pass' => $pass, 'db' => $db);
  19. }
  20.  
  21. function __destruct(){
  22. if(is_resource(self::$link)) mysql_close(self::$link);
  23. }
  24.  
  25. /**
  26. * Setter method
  27. */
  28.  
  29. static private function set($field, $value){
  30. self::$info[$field] = $value;
  31. }
  32.  
  33. /**
  34. * Getter methods
  35. */
  36.  
  37. public function last_query(){
  38. return self::$info['last_query'];
  39. }
  40.  
  41. public function num_rows(){
  42. return self::$info['num_rows'];
  43. }
  44.  
  45. public function insert_id(){
  46. return self::$info['insert_id'];
  47. }
  48.  
  49. /**
  50. * Create or return a connection to the MySQL server.
  51. */
  52.  
  53. static private function connection(){
  54. if(!is_resource(self::$link) || empty(self::$link)){
  55. if(($link = mysql_connect(self::$connection_info['host'], self::$connection_info['user'], self::$connection_info['pass'])) && mysql_select_db(self::$connection_info['db'], $link)){
  56. self::$link = $link;
  57. mysql_set_charset('utf8');
  58. }else{
  59. throw new Exception('Could not connect to MySQL database.');
  60. }
  61. }
  62. return self::$link;
  63. }
  64.  
  65. /**
  66. * MySQL Where methods
  67. */
  68.  
  69. static private function __where($info, $type = 'AND'){
  70. $link =& self::connection();
  71. $where = self::$where;
  72. foreach($info as $row => $value){
  73. if(empty($where)){
  74. $where = sprintf("WHERE `%s`='%s'", $row, mysql_real_escape_string($value));
  75. }else{
  76. $where .= sprintf(" %s `%s`='%s'", $type, $row, mysql_real_escape_string($value));
  77. }
  78. }
  79. self::$where = $where;
  80. }
  81.  
  82. public function where($field, $equal = null){
  83. if(is_array($field)){
  84. self::__where($field);
  85. }else{
  86. self::__where(array($field => $equal));
  87. }
  88. return $this;
  89. }
  90.  
  91. public function and_where($field, $equal = null){
  92. return $this->where($field, $equal);
  93. }
  94.  
  95. public function or_where($field, $equal = null){
  96. if(is_array($field)){
  97. self::__where($field, 'OR');
  98. }else{
  99. self::__where(array($field => $equal), 'OR');
  100. }
  101. return $this;
  102. }
  103.  
  104. /**
  105. * MySQL limit method
  106. */
  107.  
  108. public function limit($limit){
  109. self::$limit = 'LIMIT '.$limit;
  110. return $this;
  111. }
  112.  
  113. /**
  114. * MySQL Order By method
  115. */
  116.  
  117. public function order_by($by, $order_type = 'DESC'){
  118. $order = self::$order;
  119. if(is_array($by)){
  120. foreach($by as $field => $type){
  121. if(is_int($field) && !preg_match('/(DESC|desc|ASC|asc)/', $type)){
  122. $field = $type;
  123. $type = $order_type;
  124. }
  125. if(empty($order)){
  126. $order = sprintf("ORDER BY `%s` %s", $field, $type);
  127. }else{
  128. $order .= sprintf(", `%s` %s", $field, $type);
  129. }
  130. }
  131. }else{
  132. if(empty($order)){
  133. $order = sprintf("ORDER BY `%s` %s", $by, $order_type);
  134. }else{
  135. $order .= sprintf(", `%s` %s", $by, $order_type);
  136. }
  137. }
  138. self::$order = $order;
  139. return $this;
  140. }
  141.  
  142. /**
  143. * MySQL query helper
  144. */
  145.  
  146. static private function extra(){
  147. $extra = '';
  148. if(!empty(self::$where)) $extra .= ' '.self::$where;
  149. if(!empty(self::$order)) $extra .= ' '.self::$order;
  150. if(!empty(self::$limit)) $extra .= ' '.self::$limit;
  151. // cleanup
  152. self::$where = null;
  153. self::$order = null;
  154. self::$limit = null;
  155. return $extra;
  156. }
  157.  
  158. /**
  159. * MySQL Query methods
  160. */
  161.  
  162. public function query($qry, $return = false){
  163. $link =& self::connection();
  164. self::set('last_query', $qry);
  165. $result = mysql_query($qry);
  166. if(is_resource($result)){
  167. self::set('num_rows', mysql_num_rows($result));
  168. }
  169. if($return){
  170. if(preg_match('/LIMIT 1/', $qry)){
  171. $data = mysql_fetch_assoc($result);
  172. mysql_free_result($result);
  173. return $data;
  174. }else{
  175. $data = array();
  176. while($row = mysql_fetch_assoc($result)){
  177. $data[] = $row;
  178. }
  179. mysql_free_result($result);
  180. return $data;
  181. }
  182. }
  183. return true;
  184. }
  185.  
  186. public function get($table, $select = '*'){
  187. $link =& self::connection();
  188. if(is_array($select)){
  189. $cols = '';
  190. foreach($select as $col){
  191. $cols .= "`{$col}`,";
  192. }
  193. $select = substr($cols, 0, -1);
  194. }
  195. $sql = sprintf("SELECT %s FROM %s%s", $select, $table, self::extra());
  196. self::set('last_query', $sql);
  197. if(!($result = mysql_query($sql))){
  198. throw new Exception('Error executing MySQL query: '.$sql.'. MySQL error '.mysql_errno().': '.mysql_error());
  199. $data = false;
  200. }elseif(is_resource($result)){
  201. $num_rows = mysql_num_rows($result);
  202. self::set('num_rows', $num_rows);
  203. if($num_rows === 0){
  204. $data = false;
  205. }elseif(preg_match('/LIMIT 1/', $sql)){
  206. $data = mysql_fetch_assoc($result);
  207. }else{
  208. $data = array();
  209. while($row = mysql_fetch_assoc($result)){
  210. $data[] = $row;
  211. }
  212. }
  213. }else{
  214. $data = false;
  215. }
  216. mysql_free_result($result);
  217. return $data;
  218. }
  219.  
  220. public function insert($table, $data){
  221. $link =& self::connection();
  222. $fields = '';
  223. $values = '';
  224. foreach($data as $col => $value){
  225. $fields .= sprintf("`%s`,", $col);
  226. $values .= sprintf("'%s',", mysql_real_escape_string($value));
  227. }
  228. $fields = substr($fields, 0, -1);
  229. $values = substr($values, 0, -1);
  230. $sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $table, $fields, $values);
  231. self::set('last_query', $sql);
  232. if(!mysql_query($sql)){
  233. throw new Exception('Error executing MySQL query: '.$sql.'. MySQL error '.mysql_errno().': '.mysql_error());
  234. }else{
  235. self::set('insert_id', mysql_insert_id());
  236. return true;
  237. }
  238. }
  239.  
  240. public function update($table, $info){
  241. if(empty(self::$where)){
  242. throw new Exception("Where is not set. Can't update whole table.");
  243. }else{
  244. $link =& self::connection();
  245. $update = '';
  246. foreach($info as $col => $value){
  247. $update .= sprintf("`%s`='%s', ", $col, mysql_real_escape_string($value));
  248. }
  249. $update = substr($update, 0, -2);
  250. $sql = sprintf("UPDATE %s SET %s%s", $table, $update, self::extra());
  251. self::set('last_query', $sql);
  252. if(!mysql_query($sql)){
  253. throw new Exception('Error executing MySQL query: '.$sql.'. MySQL error '.mysql_errno().': '.mysql_error());
  254. }else{
  255. return true;
  256. }
  257. }
  258. }
  259.  
  260. public function delete($table){
  261. if(empty(self::$where)){
  262. throw new Exception("Where is not set. Can't delete whole table.");
  263. }else{
  264. $link =& self::connection();
  265. $sql = sprintf("DELETE FROM %s%s", $table, self::extra());
  266. self::set('last_query', $sql);
  267. if(!mysql_query($sql)){
  268. throw new Exception('Error executing MySQL query: '.$sql.'. MySQL error '.mysql_errno().': '.mysql_error());
  269. }else{
  270. return true;
  271. }
  272. }
  273. }
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement