* @Version 1.0 * @Package Database */ class selectDB extends myDatabaseConn { private $result = array(); // Any results from a query will be stored here private $myQuery = "";// used for debugging process with SQL return private $numResults = "";// used for returning the number of rows // Function to SELECT from the database public function select($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null){ // Create query from the variables passed to the function $q = 'SELECT '.$rows.' FROM '.$table; if($join != null){ $q .= ' JOIN '.$join; } if($where != null){ $q .= ' WHERE '.$where; } if($order != null){ $q .= ' ORDER BY '.$order; } if($limit != null){ $q .= ' LIMIT '.$limit; } // echo $table; print $this->myQuery = $q; // Pass back the SQL // Check to see if the table exists if($this->tableExists($table)){ // The table exists, run the query print $query = $this->myconn->query($q); if($query){ // If the query returns >= 1 assign the number of rows to numResults $this->numResults = $query->num_rows; // Loop through the query results by the number of rows returned for($i = 0; $i < $this->numResults; $i++){ $r = $query->fetch_array(); $key = array_keys($r); for($x = 0; $x < count($key); $x++){ // Sanitizes keys so only alphavalues are allowed if(!is_int($key[$x])){ if($query->num_rows >= 1){ $this->result[$i][$key[$x]] = $r[$key[$x]]; }else{ $this->result[$i][$key[$x]] = null; } } } } return true; // Query was successful }else{ array_push($this->result,$this->myconn->error); return false; // No rows where returned } }else{ return false; // Table does not exist } } public function selectRowCnt($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null){ // Create query from the variables passed to the function $q = 'SELECT '.$rows.' FROM '.$table; if($join != null){ $q .= ' JOIN '.$join; } if($where != null){ $q .= ' WHERE '.$where; } if($order != null){ $q .= ' ORDER BY '.$order; } if($limit != null){ $q .= ' LIMIT '.$limit; } // echo $table; $this->myQuery = $q; // Pass back the SQL // Check to see if the table exists if($this->tableExists($table)){ // The table exists, run the query $query = $this->myconn->query($q); if($query){ // If the query returns >= 1 assign the number of rows to numResults $this->numResults = $query->num_rows; return true; // Query was successful }else{ return false; // No rows where returned } }else{ return false; // Table does not exist } } // Private function to check if table exists for use with queries private function tableExists($table){ $tablesInDb = $db->myconn->query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"'); if($tablesInDb){ if($tablesInDb->num_rows == 1){ return true; // The table exists }else{ array_push($this->result,$table." does not exist in this database"); return false; // The table does not exist } } } // Public function to return the data to the user public function getResult(){ $val = $this->result; $this->result = array(); return $val; } //Pass the SQL back for debugging public function getSql(){ $val = $this->myQuery; $this->myQuery = array(); return $val; } //Pass the number of rows back public function numRows(){ $val = $this->numResults; $this->numResults = array(); return $val; } // Escape your string public function escapeString($data){ return $this->myconn->real_escape_string($data); } }