Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class mysql{//class start
- //MySQL host
- public $host = DB_HOST;
- //MySQL username
- public $user = DB_USER;
- //MySQL password
- public $pass = DB_PASSWORD;
- //MySQL database name
- public $db = DB_NAME;
- //MySQL connection
- public $con;
- //File name for tar archiver
- public $tarfilename = '';
- /**
- * Constructor to connect and select the db
- *
- * @access public
- */
- function __construct(){
- //connect to the db
- $this->con = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Could not connect to the database: <br />".$this->error());
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_select_db()
- *
- * @access public
- */
- function selectDb($db){
- mysqli_select_db($this->con, $db) or die("Could not select the database: <br/>".$this->error());;
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_query()
- *
- * @access public
- * @param string
- * @return resource or bool
- */
- function query($query){
- $res = mysqli_query($this->con, $query)/* or die("Could not query the database: <br /> $query <br/>".$this->error())*/;
- return $res;
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_affected_rows() - For SELECT statements mysqli_affected_rows() works like mysqli_num_rows().
- *
- * @access public
- * @return int
- */
- function affectedRows(){
- return mysqli_affected_rows($this->con);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_error()
- *
- * @access public
- * @return string
- */
- function error(){
- return mysqli_error($this->con);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_close()
- *
- * @access public
- */
- function close(){
- mysqli_close($this->con);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_num_rows()
- *
- * @access public
- * @param resource
- * @return int
- */
- function numRows($res){
- return mysqli_num_rows($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_num_fields() - just like mysqli_num_rows
- *
- * @access public
- * @param resource
- * @return int
- */
- function numFields($res){
- return mysqli_num_fields($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_fetch_lengths() - fetches the length of the content
- * of each column of the particular row selected by
- * mysqli_fetch_row/array/object(if these functions are
- * not called before then mysqli_fetch_lengths returns false)
- *
- * @access public
- * @param resource
- * @return array
- */
- function fetchLengths($res){
- return mysqli_fetch_lengths($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_free_result() - frees the memory of the variable
- * containing the mysql result object. for eg-
- * $result = mysqli_query($link, "SELECT * FROM tbl_name")
- * . It doesnt makes the variable null like unset(). It only frees the memory.
- *
- * @access public
- * @param resource
- */
- function freeResult($res){
- mysqli_free_result($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_fetch_row()
- *
- * @access public
- * @param resource
- * @return array
- */
- function fetchRow($res){
- return mysqli_fetch_row($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_fetch_assoc()
- *
- * @access public
- * @param resource
- * @return array
- */
- function fetchAssoc($res){
- return mysqli_fetch_assoc($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysql_fetch_array() - It is = mysqli_fetch_assoc + mysqli_fetch_row :)
- *
- * @access public
- * @param resource
- * @return array
- */
- function fetchArray($res){
- return mysqli_fetch_array($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_fetch_field() - like mysqli_fetch_array/row/assoc
- *
- * @access public
- * @param resource
- * @return array
- */
- function fetchField($res){
- return mysqli_fetch_field($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_fetch_fields() - returns an array of objects(each representing a field/column. like mysqli_fetch_all
- *
- * @access public
- * @param resource
- * @return array
- */
- function fetchFields($res){
- return mysqli_fetch_fields($res);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_data_seek() - seeks to an arbitrary result pointer(row)
- * specified by the offset(integer) in the result set. after
- * calling this function mysqli_fetch_array/assoc/row/object has
- * to be called to fetch data.
- *
- * @access public
- * @param resource
- * @param int
- * @return array
- */
- function dataSeek($res, $offset){
- return mysqli_data_seek($res, $offset);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_field_seek() - Set result pointer to a specified field offset(integer)
- *
- * @access public
- * @param resource
- * @param int
- * @return array
- */
- function fieldSeek($res, $offset){
- return mysqli_field_seek($res, $offset);
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_real_escape_string()
- *
- * @access public
- * @param string
- * @return string
- */
- function mres($str){
- return mysqli_real_escape_string($this->con, $str);
- }
- // --------------------------------------------------------------------
- /**
- * mysql_insert_id() - get the last id inserted(auto incrementing)
- *
- * @access public
- * @return int
- */
- function insertId(){
- return mysqli_insert_id($this->con);
- }
- // --------------------------------------------------------------------
- /**
- * Get/Count the no. of fields/columns in a table
- *
- * @access public
- * @param string
- * @return int
- */
- function fieldCount($table){
- $query = "SELECT * FROM $table";
- $result = $this->query($query);
- return $r = $this->numFields($result);
- }
- // --------------------------------------------------------------------
- /**
- * Check whether a particular value is present in a particular field of a particular table
- *
- * @access public
- * @param string
- * @param string
- * @param string
- * @return bool
- */
- function inTable($value, $field, $table){
- $results = $this->query("SELECT $field FROM $table WHERE $field = '$value'");
- if($this->numRows($results)) return TRUE;
- else return FALSE;
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_real_escape_string all the values present in $_POST so that
- * it can be inserted into the db safely
- *
- * @access public
- * @return array
- */
- function gatherInput($arr = NULL){
- if($arr === NULL) $arr = $_POST;
- $gatherinput;
- foreach($arr as $key => $val){
- $val = trim($val);
- if(get_magic_quotes_gpc()) $val = stripslashes($val);
- //$gatherinput[$key] = $val;
- $gatherinput[$key] = $this->mres($val);
- }
- return $gatherinput;
- }
- // --------------------------------------------------------------------
- /**
- * mysqli_real_escape_string all the values present in an array(mainly $_POST
- * or those from db obtained my mysqli_fetch_assoc) so that it can be outputed safely
- *
- * @access public
- * @return array
- */
- function gatherOutput($array, $strip = 'strip'){
- $gatheroutput;
- foreach($array as $key => $val){
- $val = trim($val);
- if(get_magic_quotes_gpc() && $strip == 'strip') $val = stripslashes($val);
- //$gatheroutput[$key] = $val;
- $gatheroutput[$key] = htmlentities($val, ENT_QUOTES);
- }
- return $gatheroutput;
- }
- // --------------------------------------------------------------------
- function count($table_name, $where = '', $start = NULL, $limit = NULL){
- if($start || $limit){
- $start .= ',';
- $res = $this->fetchAssoc($this->query("SELECT COUNT(*) AS rows FROM $table_name $where LIMIT $start $limit"));
- }
- else $res = $this->fetchAssoc($this->query("SELECT COUNT(*) AS rows FROM $table_name $where"));
- return $res['rows'];
- }
- function countryList($table = 'countries', $fields = 'iso, printable_name', $orderby = 'name'){
- $res = $this->query("SELECT $fields FROM $table ORDER BY $orderby");
- while($row = $this->fetchAssoc($res)){
- $row = $this->gatherOutput($row);
- $countries[] = $row;
- }
- return $countries;
- }
- function getSearchStr($arr){
- $str = '';
- $op = '';
- foreach($arr as $key => $val){
- if(!$val) continue;
- $str .= " $op $key = '$val'";
- $op = 'AND';
- }
- return trim($str);
- }
- }//class end
Add Comment
Please, Sign In to add comment