/dplx-include/classes/callDB.php
https://bitbucket.org/pnm123/dynaport-lite-x · PHP · 151 lines · 125 code · 17 blank · 9 comment · 21 complexity · 82063d3601bf10307d6d5d4fd0c86247 MD5 · raw file
- <?php
-
- /*
- * DynaPort Lite X
- * https://bitbucket.org/pnm123/dynaport-lite-x
- *
- * Developed by Prasad Nayanajith Manage
- * Email : prasad.n@dynamiccodes.com
- * Web : http://www.dynamiccodes.com
- *
- */
-
- class callDB {
-
- function __construct(){
- global $global_stats;
-
- if($global_stats['classes_info']['callDB']==0){
- $global_stats['classes']++;
- }
- $global_stats['classes_info']['callDB']++;
-
- $this->Connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
- $this->selectDB(DB_NAME);
- }
-
- function Connect($host='',$username='',$password=''){
- global $con;
- if(empty($host) || empty($username)){
- throwError('DB Error #1',false);
- }else{
- $con = @mysql_connect($host,$username,$password);
- if(!$con){
- throwError('DB Error #2',false,mysql_error());
- }else{
- return true;
- }
- }
- }
-
- function selectDB($dbname){
- global $con;
- $select_db = @mysql_select_db($dbname,$con);
- if(!$select_db){
- throwError('DB Error #3: Unable to select the database',false);
- }else{
- return true;
- }
- }
-
- function Query($query,$vals=''){
- global $con,$global_stats;
- if(empty($query)){
- throwError('DB Error #4: MySQL query is empty',true);
- }else{
- if(!empty($vals) && is_array($vals)){
- preg_match_all('/:([a-z0-9_-]+)/',$query,$query_inputs);
- $i=0;
- foreach($query_inputs[1] AS $pat){
- $rep = $vals[$i];
- if($pat=='_num_'){
- $rep = preg_replace('@[^(0-9)]+@','',$rep);
- }else if($pat=='_name_'){
- $rep = preg_replace(array('@[^(a-z0-9\'\s)]+@i','@\'@i'),array('','''),$rep);
- }else{
- $rep = htmlspecialchars($rep,ENT_QUOTES);
- }
- $query = preg_replace('@:'.$pat.'@',$rep,$query,1);
- $i++;
- }
- }
- $this->set->lastQuery = $query;
- $result = @mysql_query($query,$con);
- if(!$result){
- throwError('DB Error #5: MySQL query have returned an error',true,mysql_error().'<br>'.$query);
- }else{
- $global_stats['dbqueries']++;
- $global_stats['dbqueries_info'][] = $query;
- $this->set->lastResult = $result;
- $this->set->lastId = @mysql_insert_id();
- return $this->set->lastResult;
- }
- }
- }
-
- function QuerySelect($table,$where='',$group='',$order='',$limit=''){
- $query = "SELECT * FROM {$table}";
- if(!empty($where)){
- $query.= " WHERE";
- if(is_array($where)){
- $query.= " {$where[0]}";
- }else{
- $query.= " {$where}";
- }
- }
- if(!empty($group)){
- $query.= " GROUP BY {$group}";
- }
- if(!empty($order)){
- $query.= " ORDER BY {$order}";
- }
- if(!empty($limit)){
- $query.= " LIMIT {$limit}";
- }
- if(!empty($where) && is_array($where)){
- return $this->Query($query,$where[1]);
- }else{
- return $this->Query($query);
- }
- }
-
- function Num($result=''){
- if(empty($result)){
- $result = $this->set->lastResult;
- }
- return mysql_num_rows($result);
- }
-
- function Id(){
- return $this->set->lastId;
- }
-
- function Affected(){
- return mysql_affected_rows();
- }
-
- function Fetch($result=''){
- if(empty($result)){
- $result = $this->set->lastResult;
- }
- $row = mysql_fetch_assoc($result);
- return $row;
- }
-
- function FetchAll($result=''){
- if(empty($result)){
- $result = $this->set->lastResult;
- }
- while($row=mysql_fetch_assoc($result)){
- echo $results[] = $row;
- }
- return $results;
- }
-
- function lastQuery(){
- return $this->set->lastQuery;
- }
-
- }
-
- ?>