/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

  1. <?php
  2. /*
  3. * DynaPort Lite X
  4. * https://bitbucket.org/pnm123/dynaport-lite-x
  5. *
  6. * Developed by Prasad Nayanajith Manage
  7. * Email : prasad.n@dynamiccodes.com
  8. * Web : http://www.dynamiccodes.com
  9. *
  10. */
  11. class callDB {
  12. function __construct(){
  13. global $global_stats;
  14. if($global_stats['classes_info']['callDB']==0){
  15. $global_stats['classes']++;
  16. }
  17. $global_stats['classes_info']['callDB']++;
  18. $this->Connect(DB_HOST,DB_USERNAME,DB_PASSWORD);
  19. $this->selectDB(DB_NAME);
  20. }
  21. function Connect($host='',$username='',$password=''){
  22. global $con;
  23. if(empty($host) || empty($username)){
  24. throwError('DB Error #1',false);
  25. }else{
  26. $con = @mysql_connect($host,$username,$password);
  27. if(!$con){
  28. throwError('DB Error #2',false,mysql_error());
  29. }else{
  30. return true;
  31. }
  32. }
  33. }
  34. function selectDB($dbname){
  35. global $con;
  36. $select_db = @mysql_select_db($dbname,$con);
  37. if(!$select_db){
  38. throwError('DB Error #3: Unable to select the database',false);
  39. }else{
  40. return true;
  41. }
  42. }
  43. function Query($query,$vals=''){
  44. global $con,$global_stats;
  45. if(empty($query)){
  46. throwError('DB Error #4: MySQL query is empty',true);
  47. }else{
  48. if(!empty($vals) && is_array($vals)){
  49. preg_match_all('/:([a-z0-9_-]+)/',$query,$query_inputs);
  50. $i=0;
  51. foreach($query_inputs[1] AS $pat){
  52. $rep = $vals[$i];
  53. if($pat=='_num_'){
  54. $rep = preg_replace('@[^(0-9)]+@','',$rep);
  55. }else if($pat=='_name_'){
  56. $rep = preg_replace(array('@[^(a-z0-9\'\s)]+@i','@\'@i'),array('','&#039;'),$rep);
  57. }else{
  58. $rep = htmlspecialchars($rep,ENT_QUOTES);
  59. }
  60. $query = preg_replace('@:'.$pat.'@',$rep,$query,1);
  61. $i++;
  62. }
  63. }
  64. $this->set->lastQuery = $query;
  65. $result = @mysql_query($query,$con);
  66. if(!$result){
  67. throwError('DB Error #5: MySQL query have returned an error',true,mysql_error().'<br>'.$query);
  68. }else{
  69. $global_stats['dbqueries']++;
  70. $global_stats['dbqueries_info'][] = $query;
  71. $this->set->lastResult = $result;
  72. $this->set->lastId = @mysql_insert_id();
  73. return $this->set->lastResult;
  74. }
  75. }
  76. }
  77. function QuerySelect($table,$where='',$group='',$order='',$limit=''){
  78. $query = "SELECT * FROM {$table}";
  79. if(!empty($where)){
  80. $query.= " WHERE";
  81. if(is_array($where)){
  82. $query.= " {$where[0]}";
  83. }else{
  84. $query.= " {$where}";
  85. }
  86. }
  87. if(!empty($group)){
  88. $query.= " GROUP BY {$group}";
  89. }
  90. if(!empty($order)){
  91. $query.= " ORDER BY {$order}";
  92. }
  93. if(!empty($limit)){
  94. $query.= " LIMIT {$limit}";
  95. }
  96. if(!empty($where) && is_array($where)){
  97. return $this->Query($query,$where[1]);
  98. }else{
  99. return $this->Query($query);
  100. }
  101. }
  102. function Num($result=''){
  103. if(empty($result)){
  104. $result = $this->set->lastResult;
  105. }
  106. return mysql_num_rows($result);
  107. }
  108. function Id(){
  109. return $this->set->lastId;
  110. }
  111. function Affected(){
  112. return mysql_affected_rows();
  113. }
  114. function Fetch($result=''){
  115. if(empty($result)){
  116. $result = $this->set->lastResult;
  117. }
  118. $row = mysql_fetch_assoc($result);
  119. return $row;
  120. }
  121. function FetchAll($result=''){
  122. if(empty($result)){
  123. $result = $this->set->lastResult;
  124. }
  125. while($row=mysql_fetch_assoc($result)){
  126. echo $results[] = $row;
  127. }
  128. return $results;
  129. }
  130. function lastQuery(){
  131. return $this->set->lastQuery;
  132. }
  133. }
  134. ?>