Guest User

Untitled

a guest
Apr 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. <?php
  2.  
  3. class mysql
  4. {
  5.  
  6. public $conn = "";
  7. public $debug = 0;
  8. public $queries;
  9.  
  10. public function mysql( $dbUser = "user", $dbPass = "pass", $dbName = "database", $dbHost = "localhost" )
  11. {
  12. global $config;
  13. $this->user = $dbUser;
  14. $this->pass = $dbPass;
  15. $this->name = $dbName;
  16. $this->host = $dbHost;
  17. if ( $this->debug == 1 )
  18. {
  19. $this->queries = array( );
  20. $this->comments = array( );
  21. }
  22. $this->last_result = FALSE;
  23. $this->debug = $config['debug'];
  24. return TRUE;
  25. }
  26.  
  27. public function connect( )
  28. {
  29. if ( !$this->conn = mysql_connect( $this->host, $this->user, $this->pass ) )
  30. {
  31. exit( "ERR_DB_CONNECT" );
  32. }
  33. $this->name( $this->name );
  34. return $this->conn;
  35. }
  36.  
  37. public function select_db( $db )
  38. {
  39. if ( !mysql_select_db( $db, $this->conn ) )
  40. {
  41. exit( "ERR_MYSQL_SELECT_DB" );
  42. }
  43. $this->query( "set names utf8" );
  44. }
  45.  
  46. public function query( $query, $comment = "" )
  47. {
  48. if ( $this->conn )
  49. {
  50. $this->conn = $this->connect( );
  51. }
  52. $start = microtime( );
  53. if ( !( $result = mysql_query( $query, $this->conn ) ) )
  54. {
  55. exit( mysql_error( ) );
  56. }
  57. $end = microtime( );
  58. if ( $this->debug == 1 )
  59. {
  60. # $sec1 = explode( " ", $start )[1];
  61. # $usec1 = explode( " ", $start )[0];
  62. # $sec2 = explode( " ", $end )[1];
  63. # $usec2 = explode( " ", $end )[0];
  64. $diff = round( $sec2 - $sec1 + $usec2 - $usec1, 5 );
  65. $this->queries[] = $query;
  66. $this->comments[] = $comment;
  67. $this->queries['time'][] = $diff;
  68. }
  69. $this->last_result = $result;
  70. return $result;
  71. }
  72.  
  73. public function fetch_object( $res = FALSE )
  74. {
  75. $res = $res ? $res : $this->last_result;
  76. return mysql_fetch_object( $res );
  77. }
  78.  
  79. public function fetch_array( $res = FALSE, $type = "name" )
  80. {
  81. $res = $res ? $res : $this->last_result;
  82. if ( $type == "name" )
  83. {
  84. return mysql_fetch_array( $res, MYSQL_ASSOC );
  85. }
  86. return mysql_fetch_array( $res, MYSQL_NUM );
  87. }
  88.  
  89. public function num_rows( $res = FALSE )
  90. {
  91. $res = $res ? $res : $this->last_result;
  92. return mysql_num_rows( $res );
  93. }
  94.  
  95. public function insert_id( )
  96. {
  97. return mysql_insert_id( );
  98. }
  99.  
  100. public function affected_rows( )
  101. {
  102. return mysql_affected_rows( );
  103. }
  104.  
  105. public function close( )
  106. {
  107. if ( $this->conn )
  108. {
  109. mysql_close( $this->conn );
  110. }
  111. }
  112.  
  113. public function print_queries( )
  114. {
  115. $sumTime = 0;
  116. if ( is_array( $this->queries['time'] ) )
  117. {
  118. $sumTime = array_sum( $this->queries['time'] );
  119. }
  120. @$html = "<hr style=\"clear: both\"/><div style=\"margin-left: 10px;\">";
  121. @$html .= @"<div style=\"color: red; text-decoration: underline; margin-bottom: 5px;\">Queries: <b>".@count( @$this->queries['time'] )."</b>; queriesExecuteTime: <b>".$sumTime."</b></div>";
  122. if ( empty( $this->queries['time'] ) )
  123. {
  124. foreach ( $this->queries['time'] as $key => $value )
  125. {
  126. $key2 = $key + 1;
  127. $html .= nl2br( "#".$key2." - ".$this->queries[$key]( $this->queries[$key] ) ).";( <b><font color=yellow>".$this->comments[$key]."</b></font> ) <b>�����: <font color=blue>".sprintf( "%.5f", $this->queries['time'][$key] )."</font></b><hr>";
  128. }
  129. }
  130. $html .= "</div>";
  131. return $html;
  132. }
  133.  
  134. public function getFoundRows( )
  135. {
  136. $this->query( "\n\t\t\tSELECT FOUND_ROWS() AS f_rows\n\t\t" );
  137. $d = $this->fetch_array( );
  138. return $d['f_rows'];
  139. }
  140.  
  141. public function getRowCount( )
  142. {
  143. $this->query( "\n\t\t\tSELECT ROW_COUNT() AS f_rows\n\t\t" );
  144. $d = $this->fetch_array( );
  145. return $d['f_rows'];
  146. }
  147.  
  148. public function queryColor( $query )
  149. {
  150. $query = preg_replace( "/['\"]([^'\"]*)['\"]/i", "'<FONT COLOR='#FF6600'>$1</FONT>'", $query, -1 );
  151. $query = str_ireplace( array( "*", "SELECT", "UPDATE ", "DELETE ", "INSERT ", "INTO", "VALUES", "FROM", "LEFT", "JOIN", "WHERE", "LIMIT", "ORDER BY", "GROUP BY", "AND", "OR ", "DESC", "ASC", "ON " ), array( "<FONT COLOR='#FF6600'><B>*</B></FONT>", "<FONT COLOR='#00AA00'><B>SELECT</B> </FONT>", "<FONT COLOR='#00AA00'><B>UPDATE</B> </FONT>", "<FONT COLOR='#00AA00'><B>DELETE</B> </FONT>", "<FONT COLOR='#00AA00'><B>INSERT</B> </FONT>", "<FONT COLOR='#00AA00'><B>INTO</B></FONT>", "<FONT COLOR='#00AA00'><B>VALUES</B></FONT>", "<FONT COLOR='#00AA00'><B>FROM</B></FONT>", "<FONT COLOR='#00CC00'><B>LEFT</B></FONT>", "<FONT COLOR='#00CC00'><B>JOIN</B></FONT>", "<FONT COLOR='#00AA00'><B>WHERE</B></FONT>", "<FONT COLOR='#AA0000'><B>LIMIT</B></FONT>", "<FONT COLOR='#00AA00'><B>ORDER BY</B></FONT>", "<FONT COLOR='#00AA00'><B>GROUP BY</B></FONT>", "<FONT COLOR='#0000AA'><B>AND</B></FONT>", "<FONT COLOR='#0000AA'><B>OR</B> </FONT>", "<FONT COLOR='#0000AA'><B>DESC</B></FONT>", "<FONT COLOR='#0000AA'><B>ASC</B></FONT>", "<FONT COLOR='#00DD00'><B>ON</B> </FONT>" ), $query );
  152. return $query;
  153. }
  154.  
  155. public function getRow( $query )
  156. {
  157. $result = $this->query( $query );
  158. if ( mysql_num_rows( $result ) == 0 )
  159. {
  160. return FALSE;
  161. }
  162. return $this->fetch_array( );
  163. }
  164.  
  165. public function getCol( $query )
  166. {
  167. $result = $this->query( $query );
  168. if ( mysql_num_rows( $result ) == 0 )
  169. {
  170. return FALSE;
  171. }
  172. while ( $row = mysql_fetch_array( $result ) )
  173. {
  174. $rows[] = $row[0];
  175. }
  176. return $rows;
  177. }
  178.  
  179. public function getValue( $query )
  180. {
  181. $result = $this->query( $query );
  182. if ( mysql_num_rows( $result ) == 0 )
  183. {
  184. return FALSE;
  185. }
  186. $row = mysql_fetch_array( $result );
  187. return $row[0];
  188. }
  189.  
  190. public function insert( $tableName, $array )
  191. {
  192. if ( is_array( $array ) )
  193. {
  194. $arrayKeys = array_keys( $array );
  195. foreach ( $arrayKeys as $value )
  196. {
  197. if ( strtolower( $value ) == "pk" )
  198. {
  199. $arrayKeys2 = array_keys( $array[$value] );
  200. foreach ( $arrayKeys2 as $value2 )
  201. {
  202. $insertArray[$value2] = $array[$value][$value2];
  203. }
  204. }
  205. else if ( strtolower( $value ) == "iu" )
  206. {
  207. $arrayKeys2 = array_keys( $array[$value] );
  208. foreach ( $arrayKeys2 as $value2 )
  209. {
  210. $insertArray[$value2] = $array[$value][$value2];
  211. }
  212. }
  213. }
  214. $columnName = "";
  215. $columnValue = "";
  216. $arrayKeys = array_keys( $insertArray );
  217. foreach ( $arrayKeys as $value )
  218. {
  219. if ( $columnName != "" )
  220. {
  221. $columnName .= ", ";
  222. $columnValue .= ", ";
  223. }
  224. if ( $insertArray[$value] != "NOW()" && $insertArray[$value] != "NULL" )
  225. {
  226. $columnValue .= " '".sql_quote( $insertArray[$value] )."' ";
  227. }
  228. else
  229. {
  230. $columnValue .= " ".$insertArray[$value]." ";
  231. }
  232. $columnName .= " `".sql_quote( $value )."` ";
  233. }
  234. $this->query( "INSERT INTO `".$tableName."`({$columnName}) VALUES({$columnValue})" );
  235. return TRUE;
  236. }
  237. return FALSE;
  238. }
  239.  
  240. public function update( $tableName, $array )
  241. {
  242. if ( is_array( $array ) )
  243. {
  244. $setQuery = "";
  245. $whereQuery = "";
  246. $arrayKeys = array_keys( $array );
  247. foreach ( $arrayKeys as $value )
  248. {
  249. if ( strtolower( $value ) == "pk" )
  250. {
  251. $arrayKeys2 = array_keys( $array[$value] );
  252. foreach ( $arrayKeys2 as $value2 )
  253. {
  254. if ( $whereQuery == "" )
  255. {
  256. $whereQuery = "`".$value2."` = '".$array[$value][$value2]."'";
  257. }
  258. else
  259. {
  260. $whereQuery .= " AND `".$value2."` = '".$array[$value][$value2]."'";
  261. }
  262. }
  263. }
  264. else if ( strtolower( $value ) == "iu" )
  265. {
  266. $arrayKeys2 = array_keys( $array[$value] );
  267. foreach ( $arrayKeys2 as $value2 )
  268. {
  269. if ( $array[$value][$value2] == "NULL" || $array[$value][$value2] == "NOW()" )
  270. {
  271. $updateValue = sql_quote( $array[$value][$value2] );
  272. }
  273. else
  274. {
  275. $updateValue = "'".sql_quote( $array[$value][$value2] )."'";
  276. }
  277. if ( $setQuery == "" )
  278. {
  279. $setQuery = "`".$value2."` = {$updateValue}";
  280. }
  281. else
  282. {
  283. $setQuery .= ", `".$value2."` = {$updateValue}";
  284. }
  285. }
  286. }
  287. }
  288. $this->query( "UPDATE `".$tableName."` SET {$setQuery} WHERE {$whereQuery}" );
  289. return TRUE;
  290. }
  291. return FALSE;
  292. }
  293.  
  294. public function getAll( $query, $field = "" )
  295. {
  296. $this->query( $query, "getAll" );
  297. if ( $this->num_rows( ) < 1 )
  298. {
  299. return FALSE;
  300. }
  301. $return = array( );
  302. while ( $d = $this->fetch_array( ) )
  303. {
  304. if ( $field == "" )
  305. {
  306. $return[] = $d;
  307. }
  308. else
  309. {
  310. $return[$d[$field]] = $d;
  311. }
  312. }
  313. return $return;
  314. }
  315.  
  316. }
  317.  
  318. ?>
Add Comment
Please, Sign In to add comment