Guest User

Untitled

a guest
Jul 31st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. <?php
  2.  
  3. class mysql{//class start
  4.  
  5. //MySQL host
  6. public $host = DB_HOST;
  7.  
  8. //MySQL username
  9. public $user = DB_USER;
  10.  
  11. //MySQL password
  12. public $pass = DB_PASSWORD;
  13.  
  14. //MySQL database name
  15. public $db = DB_NAME;
  16.  
  17. //MySQL connection
  18. public $con;
  19.  
  20. //File name for tar archiver
  21. public $tarfilename = '';
  22.  
  23. /**
  24. * Constructor to connect and select the db
  25. *
  26. * @access public
  27. */
  28. function __construct(){
  29. //connect to the db
  30. $this->con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Could not connect to the database: <br />".$this->error());
  31. }
  32.  
  33. // --------------------------------------------------------------------
  34.  
  35. /**
  36. * mysqli_select_db()
  37. *
  38. * @access public
  39. */
  40. function selectDb($db){
  41. mysqli_select_db($this->con, $db) or die("Could not select the database: <br/>".$this->error());;
  42. }
  43.  
  44. // --------------------------------------------------------------------
  45.  
  46. /**
  47. * mysqli_query()
  48. *
  49. * @access public
  50. * @param string
  51. * @return resource or bool
  52. */
  53. function query($query){
  54. $res = mysqli_query($this->con, $query)/* or die("Could not query the database: <br /> $query <br/>".$this->error())*/;
  55. return $res;
  56. }
  57.  
  58. // --------------------------------------------------------------------
  59.  
  60. /**
  61. * mysqli_affected_rows() - For SELECT statements mysqli_affected_rows() works like mysqli_num_rows().
  62. *
  63. * @access public
  64. * @return int
  65. */
  66. function affectedRows(){
  67. return mysqli_affected_rows($this->con);
  68. }
  69.  
  70. // --------------------------------------------------------------------
  71.  
  72. /**
  73. * mysqli_error()
  74. *
  75. * @access public
  76. * @return string
  77. */
  78. function error(){
  79. return mysqli_error($this->con);
  80. }
  81.  
  82. // --------------------------------------------------------------------
  83.  
  84. /**
  85. * mysqli_close()
  86. *
  87. * @access public
  88. */
  89. function close(){
  90. mysqli_close($this->con);
  91. }
  92.  
  93. // --------------------------------------------------------------------
  94.  
  95. /**
  96. * mysqli_num_rows()
  97. *
  98. * @access public
  99. * @param resource
  100. * @return int
  101. */
  102. function numRows($res){
  103. return mysqli_num_rows($res);
  104. }
  105.  
  106. // --------------------------------------------------------------------
  107.  
  108. /**
  109. * mysqli_num_fields() - just like mysqli_num_rows
  110. *
  111. * @access public
  112. * @param resource
  113. * @return int
  114. */
  115. function numFields($res){
  116. return mysqli_num_fields($res);
  117. }
  118.  
  119. // --------------------------------------------------------------------
  120.  
  121. /**
  122. * mysqli_fetch_lengths() - fetches the length of the content
  123. * of each column of the particular row selected by
  124. * mysqli_fetch_row/array/object(if these functions are
  125. * not called before then mysqli_fetch_lengths returns false)
  126. *
  127. * @access public
  128. * @param resource
  129. * @return array
  130. */
  131. function fetchLengths($res){
  132. return mysqli_fetch_lengths($res);
  133. }
  134.  
  135. // --------------------------------------------------------------------
  136.  
  137. /**
  138. * mysqli_free_result() - frees the memory of the variable
  139. * containing the mysql result object. for eg-
  140. * $result = mysqli_query($link, "SELECT * FROM tbl_name")
  141. * . It doesnt makes the variable null like unset(). It only frees the memory.
  142. *
  143. * @access public
  144. * @param resource
  145. */
  146. function freeResult($res){
  147. mysqli_free_result($res);
  148. }
  149.  
  150. // --------------------------------------------------------------------
  151.  
  152. /**
  153. * mysqli_fetch_row()
  154. *
  155. * @access public
  156. * @param resource
  157. * @return array
  158. */
  159. function fetchRow($res){
  160. return mysqli_fetch_row($res);
  161. }
  162.  
  163. // --------------------------------------------------------------------
  164.  
  165. /**
  166. * mysqli_fetch_assoc()
  167. *
  168. * @access public
  169. * @param resource
  170. * @return array
  171. */
  172. function fetchAssoc($res){
  173. return mysqli_fetch_assoc($res);
  174. }
  175.  
  176. // --------------------------------------------------------------------
  177.  
  178. /**
  179. * mysql_fetch_array() - It is = mysqli_fetch_assoc + mysqli_fetch_row :)
  180. *
  181. * @access public
  182. * @param resource
  183. * @return array
  184. */
  185. function fetchArray($res){
  186. return mysqli_fetch_array($res);
  187. }
  188.  
  189. // --------------------------------------------------------------------
  190.  
  191. /**
  192. * mysqli_fetch_field() - like mysqli_fetch_array/row/assoc
  193. *
  194. * @access public
  195. * @param resource
  196. * @return array
  197. */
  198. function fetchField($res){
  199. return mysqli_fetch_field($res);
  200. }
  201.  
  202. // --------------------------------------------------------------------
  203.  
  204. /**
  205. * mysqli_fetch_fields() - returns an array of objects(each representing a field/column. like mysqli_fetch_all
  206. *
  207. * @access public
  208. * @param resource
  209. * @return array
  210. */
  211. function fetchFields($res){
  212. return mysqli_fetch_fields($res);
  213. }
  214.  
  215. // --------------------------------------------------------------------
  216.  
  217. /**
  218. * mysqli_data_seek() - seeks to an arbitrary result pointer(row)
  219. * specified by the offset(integer) in the result set. after
  220. * calling this function mysqli_fetch_array/assoc/row/object has
  221. * to be called to fetch data.
  222. *
  223. * @access public
  224. * @param resource
  225. * @param int
  226. * @return array
  227. */
  228. function dataSeek($res, $offset){
  229. return mysqli_data_seek($res, $offset);
  230. }
  231.  
  232. // --------------------------------------------------------------------
  233.  
  234. /**
  235. * mysqli_field_seek() - Set result pointer to a specified field offset(integer)
  236. *
  237. * @access public
  238. * @param resource
  239. * @param int
  240. * @return array
  241. */
  242. function fieldSeek($res, $offset){
  243. return mysqli_field_seek($res, $offset);
  244. }
  245.  
  246. // --------------------------------------------------------------------
  247.  
  248. /**
  249. * mysqli_real_escape_string()
  250. *
  251. * @access public
  252. * @param string
  253. * @return string
  254. */
  255. function mres($str){
  256. return mysqli_real_escape_string($this->con, $str);
  257. }
  258.  
  259. // --------------------------------------------------------------------
  260.  
  261. /**
  262. * mysql_insert_id() - get the last id inserted(auto incrementing)
  263. *
  264. * @access public
  265. * @return int
  266. */
  267. function insertId(){
  268. return mysqli_insert_id($this->con);
  269. }
  270.  
  271. // --------------------------------------------------------------------
  272.  
  273. /**
  274. * Get/Count the no. of fields/columns in a table
  275. *
  276. * @access public
  277. * @param string
  278. * @return int
  279. */
  280. function fieldCount($table){
  281. $query = "SELECT * FROM $table";
  282. $result = $this->query($query);
  283. return $r = $this->numFields($result);
  284. }
  285.  
  286. // --------------------------------------------------------------------
  287.  
  288. /**
  289. * Check whether a particular value is present in a particular field of a particular table
  290. *
  291. * @access public
  292. * @param string
  293. * @param string
  294. * @param string
  295. * @return bool
  296. */
  297. function inTable($value, $field, $table){
  298. $results = $this->query("SELECT $field FROM $table WHERE $field = '$value'");
  299. if($this->numRows($results)) return TRUE;
  300. else return FALSE;
  301. }
  302.  
  303. // --------------------------------------------------------------------
  304.  
  305. /**
  306. * mysqli_real_escape_string all the values present in $_POST so that
  307. * it can be inserted into the db safely
  308. *
  309. * @access public
  310. * @return array
  311. */
  312. function gatherInput($arr = NULL){
  313. if($arr === NULL) $arr = $_POST;
  314. $gatherinput;
  315. foreach($arr as $key => $val){
  316. $val = trim($val);
  317. if(get_magic_quotes_gpc()) $val = stripslashes($val);
  318. //$gatherinput[$key] = $val;
  319. $gatherinput[$key] = $this->mres($val);
  320. }
  321. return $gatherinput;
  322. }
  323.  
  324. // --------------------------------------------------------------------
  325.  
  326. /**
  327. * mysqli_real_escape_string all the values present in an array(mainly $_POST
  328. * or those from db obtained my mysqli_fetch_assoc) so that it can be outputed safely
  329. *
  330. * @access public
  331. * @return array
  332. */
  333. function gatherOutput($array, $strip = 'strip'){
  334. $gatheroutput;
  335. foreach($array as $key => $val){
  336. $val = trim($val);
  337. if(get_magic_quotes_gpc() && $strip == 'strip') $val = stripslashes($val);
  338. //$gatheroutput[$key] = $val;
  339. $gatheroutput[$key] = htmlentities($val, ENT_QUOTES);
  340. }
  341. return $gatheroutput;
  342. }
  343.  
  344. // --------------------------------------------------------------------
  345.  
  346. function count($table_name, $where = '', $start = NULL, $limit = NULL){
  347. if($start || $limit){
  348. $start .= ',';
  349. $res = $this->fetchAssoc($this->query("SELECT COUNT(*) AS rows FROM $table_name $where LIMIT $start $limit"));
  350. }
  351. else $res = $this->fetchAssoc($this->query("SELECT COUNT(*) AS rows FROM $table_name $where"));
  352. return $res['rows'];
  353. }
  354.  
  355. function countryList($table = 'countries', $fields = 'iso, printable_name', $orderby = 'name'){
  356. $res = $this->query("SELECT $fields FROM $table ORDER BY $orderby");
  357. while($row = $this->fetchAssoc($res)){
  358. $row = $this->gatherOutput($row);
  359. $countries[] = $row;
  360. }
  361. return $countries;
  362. }
  363.  
  364. function getSearchStr($arr){
  365. $str = '';
  366. $op = '';
  367. foreach($arr as $key => $val){
  368. if(!$val) continue;
  369. $str .= " $op $key = '$val'";
  370. $op = 'AND';
  371. }
  372. return trim($str);
  373. }
  374.  
  375. }//class end
Add Comment
Please, Sign In to add comment