Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. <?php
  2.  
  3. class db{
  4. //we may want to do more than connect via mysql;
  5. var $type="mysql";
  6.  
  7. // mysql vars;
  8. var $db_link;
  9. var $hostname;
  10. var $schema;
  11. var $dbuser;
  12. var $dbpass;
  13. var $pconnect=false;
  14.  
  15. var $showError=false;
  16. var $logError=true;
  17. var $stopOnError=true;
  18. var $errorFormat="xhtml";
  19.  
  20. var $error = NULL;
  21.  
  22. function db($connect = true, $hostname = NULL, $schema = NULL, $user = NULL, $pass = NULL, $pconnect = NULL, $type = "mysql"){
  23.  
  24. if($type!="mysql")
  25. $this->type=$type;
  26.  
  27. switch($this->type){
  28. default:
  29. case "mysql":
  30. if(defined("MYSQL_SERVER"))
  31. $this->hostname = MYSQL_SERVER;
  32. if($hostname!=NULL)
  33. $this->hostname = $hostname;
  34.  
  35. if(defined("MYSQL_DATABASE"))
  36. $this->schema = MYSQL_DATABASE;
  37. if($schema!=NULL)
  38. $this->schema = $schema;
  39.  
  40. if(defined("MYSQL_USER"))
  41. $this->dbuser = MYSQL_USER;
  42. if($schema!=NULL)
  43. $this->dbuser = $user;
  44.  
  45. if(defined("MYSQL_USERPASS"))
  46. $this->dbpass = MYSQL_USERPASS;
  47. if($schema!=NULL)
  48. $this->dbpass = $pass;
  49.  
  50. if(defined("MYSQL_PCONNECT"))
  51. $this->pconnect = MYSQL_PCONNECT;
  52. if($pconnect!=NULL)
  53. $this->pconnect = $pconnect;
  54. break;
  55. }
  56.  
  57. if($connect){
  58. if($this->connect()){
  59. if($this->selectSchema())
  60. return $this->db_link;
  61. else
  62. return false;
  63. } else
  64. return false;
  65. } else return true;
  66. }//end function
  67.  
  68.  
  69. function connect(){
  70. // This functions connects to the database. It uses pconnect if the variable is set;
  71.  
  72. if($this->pconnect) {
  73. $this->db_link = @ mysql_pconnect($this->hostname,$this->dbuser,$this->dbpass);
  74. } else {
  75. $this->db_link = @ mysql_connect($this->hostname,$this->dbuser,$this->dbpass);
  76. }
  77. if(!$this->db_link){
  78. $error = new appError(-400,"Could not connect to database server.\n\n".mysql_error(),"",$this->showError,$this->stopOnError,false,$this->errorFormat);
  79. return false;
  80. } else
  81. return $this->db_link;
  82. }
  83.  
  84.  
  85. function selectSchema($schema=NULL){
  86. if($schema!=NULL)
  87. $this->schema=$schema;
  88.  
  89. if(! @ mysql_select_db($this->schema,$this->db_link)){
  90. $error = new appError(-410,"Could not open schema ".$this->schema,"",$this->showError,$this->stopOnError,false,$this->errorFormat);
  91. return false;
  92. } else
  93. return true;
  94. }
  95.  
  96.  
  97. function query($sqlstatement){
  98. switch($this->type){
  99. case "mysql":
  100. if(!isset($this->db_link))
  101. if(!$this->dataB()) die($this->error);
  102. $queryresult= @ mysql_query($sqlstatement,$this->db_link);
  103. if(!$queryresult){
  104. $this->error = mysql_error($this->db_link);
  105. $error = new appError(-420,mysql_error($this->db_link)."\n\nStatement: ".$sqlstatement,"",$this->showError,$this->stopOnError,$this->logError,$this->errorFormat);
  106. return false;
  107. }
  108. break;
  109. }//end case
  110.  
  111. $this->error=NULL;
  112. return $queryresult;
  113. }//end function
  114.  
  115.  
  116. function setEncoding($encoding = "utf8"){
  117. switch($this->type){
  118. case "mysql":
  119. @ mysql_query("SET NAMES ".$encoding, $this->db_link);
  120. break;
  121.  
  122. }//endswitch
  123.  
  124. }//end method
  125.  
  126.  
  127. function numRows($queryresult){
  128. switch($this->type){
  129. case "mysql":
  130. $numrows=@ mysql_num_rows($queryresult);
  131. if(!is_numeric($numrows)){
  132. $error= new appError(-430,"","Could Not Retrieve Rows.","",$this->showError,$this->stopOnError,$this->logError,$this->errorFormat);
  133. return false;
  134. }
  135. break;
  136. }//end case
  137. $this->error=NULL;
  138. return $numrows;
  139. }//end function
  140.  
  141.  
  142. function fetchArray($queryresult){
  143. //Fetches associative array of current row
  144. switch($this->type){
  145. case "mysql":
  146. $row=@ mysql_fetch_assoc($queryresult);
  147. break;
  148. }//end case
  149. return $row;
  150. }//end function
  151.  
  152. function freeResult($queryresult){
  153. //free result
  154. switch($this->type){
  155. case "mysql":
  156. $thereturn=@ mysql_free_result($queryresult);
  157. break;
  158. }//end case
  159. return $thereturn;
  160. }//end function
  161.  
  162. function Close(){
  163. //free result
  164. switch($this->type){
  165. case "mysql":
  166. $thereturn=@ mysql_close($this->db_link);
  167. break;
  168. }//end case
  169. return $thereturn;
  170. }//end function
  171.  
  172. function seek($queryresult,$rownum){
  173. switch($this->type){
  174. case "mysql":
  175. $thereturn=@ mysql_data_seek($queryresult,$rownum);
  176. break;
  177. }//end case
  178. return $thereturn;
  179. }//end function
  180.  
  181.  
  182. function numFields($queryresult){
  183. switch($this->type){
  184. case "mysql":
  185. $thereturn=@ mysql_num_fields($queryresult);
  186. break;
  187. }//end case
  188. return $thereturn;
  189. }//end function
  190.  
  191.  
  192. function fieldTable($queryresult,$offset){
  193. switch($this->type){
  194. case "mysql":
  195. $thereturn=@ mysql_field_table($queryresult,$offset);
  196. break;
  197. }//end case
  198. return $thereturn;
  199. }//end function
  200.  
  201.  
  202. function fieldName($queryresult,$offset){
  203. switch($this->type){
  204. case "mysql":
  205. $thereturn=@ mysql_field_name($queryresult,$offset);
  206. break;
  207. }//end case
  208. return $thereturn;
  209. }//end function
  210.  
  211.  
  212. function tableInfo($tablename){
  213. //this function returns a multi-dimensional array describing the fields in a given table
  214. $thereturn = false;
  215. switch($this->type){
  216. case "mysql":
  217. $queryresult = @ mysql_list_fields($this->schema,$tablename);
  218. if($queryresult){
  219. for($offset = 0; $offset < mysql_num_fields($queryresult); ++$offset){
  220. $name = $this->fieldName($queryresult,$offset);
  221. $thereturn[$name]["type"] = @ mysql_field_type($queryresult,$offset);
  222. $thereturn[$name]["length"] = mysql_field_len($queryresult,$offset);
  223. $thereturn[$name]["flags"] = mysql_field_flags($queryresult,$offset);
  224. }
  225. }
  226. break;
  227. }//end case
  228. return $thereturn;
  229. }
  230.  
  231.  
  232. function insertId(){
  233. $thereturn = false;
  234. switch($this->type){
  235. case "mysql":
  236. $thereturn = @ mysql_insert_id($this->db_link);
  237. break;
  238. }
  239.  
  240. return $thereturn;
  241. }
  242.  
  243. function affectedRows(){
  244. $thereturn = false;
  245. switch($this->type){
  246. case "mysql":
  247. $thereturn = @ mysql_affected_rows($this->db_link);
  248. break;
  249. }
  250.  
  251. return $thereturn;
  252. }
  253.  
  254. }//end db class
  255.  
  256.  
  257. $querystatement = "
  258. SELECT
  259. invoices.id,
  260. clients.name as clientname,
  261. subclients.name as subclientname,
  262. invoices.groupid
  263. FROM
  264. invoices
  265. LEFT JOIN clients ON clients.id = invoices.parentid
  266. LEFT JOIN subclients ON subclients.id = invoices.clientid and invoices.clienttype=1
  267. WHERE
  268. invoices.groupid='".$_POST["groupid"]."' or invoices.groupid='".(int)$_POST["groupid"]."'";
  269.  
  270. $queryresult = $db->query($querystatement);
  271. if($db->numRows($queryresult)!=0){
  272. $therecord = $db->fetchArray($queryresult);
  273. echo "<center><h1 style=\"color: green;\">Pedido OK:</h1><br>
  274. ".$therecord["clientname"]." <br>".$therecord["subclientname"]."
  275. <hr>";
  276. } else {
  277. echo "<center><h1 style=\"color: red;\">".$_POST["groupid"]." n existe</h1><br>";
  278.  
  279. }
  280.  
  281.  
  282. $db->close();
  283. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement