Facebook
From Pavlus, 9 Years ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 1340
  1. <?php
  2.  
  3.  
  4.  
  5. /*
  6.  
  7. SimpleDB - Mysql driver class
  8.  
  9. Version: 0.2.3
  10.  
  11. Author: Pawel 'Pavlus' Janisio
  12.  
  13. License: GPL v3
  14.  
  15. SVN: http://code.google.com/p/simplemysqlclass/source/browse/#svn/
  16.  
  17. */
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. class DB_MySQL
  26.  
  27.  
  28.  
  29.         {
  30.  
  31.  
  32.  
  33. /*
  34.  
  35. Exit while error.
  36.  
  37. This variable when ednabled (1) terminates your program in case of ay error
  38.  
  39. Default value is 0
  40.  
  41. int
  42.  
  43. */
  44.  
  45.  
  46.  
  47. private $exit = 0;
  48.  
  49.  
  50.  
  51.  
  52.  
  53. protected $connection = NULL;
  54.  
  55. protected $database = NULL;
  56.  
  57. protected $error = NULL;
  58.  
  59. protected $disconnect = NULL;
  60.  
  61. private $db_host = '';
  62.  
  63. private $db_port = 0;
  64.  
  65. private $db_user = '';
  66.  
  67. private $db_password = '';
  68.  
  69. private $db_database = '';
  70.  
  71. private $fetched = array();
  72.  
  73. private $rows = 0;
  74.  
  75. private $result = NULL;
  76.  
  77. public $queries = 0;
  78.  
  79. public $errors = 0;
  80.  
  81. public $exe = NULL;
  82.  
  83.  
  84.  
  85.  
  86.  
  87.         public function __construct ($db_host, $db_port, $db_user, $db_password, $db_database, $debug_level = NULL)
  88.  
  89.                         {
  90.  
  91.                                 //assign variables
  92.  
  93.                                 $this->db_host = $db_host;
  94.  
  95.                                 $this->db_port = $db_port;
  96.  
  97.                                 $this->db_user = $db_user;
  98.  
  99.                                 $this->db_password = $db_password;
  100.  
  101.                                 $this->db_database = $db_database;
  102.  
  103.                                
  104.  
  105.                                 if(isset($debug_level))
  106.  
  107.                                 $this->debugLevel = $debug_level;
  108.  
  109.                                         else
  110.  
  111.                                                 $this->debugLevel = 1;
  112.  
  113.                
  114.  
  115.                  switch ($this->debugLevel)
  116.  
  117.                             {
  118.  
  119.                               case 0:
  120.  
  121.                                 error_reporting(0);
  122.  
  123.                               break;
  124.  
  125.                              
  126.  
  127.                               case 1:
  128.  
  129.                                 error_reporting(E_ALL ^ E_NOTICE);
  130.  
  131.                               break;
  132.  
  133.                              
  134.  
  135.                               case 2:
  136.  
  137.                                 error_reporting(E_ALL);
  138.  
  139.                               break;
  140.  
  141.                              
  142.  
  143.                                 default:
  144.  
  145.                                 $error_reporting(E_ALL ^ E_NOTICE);
  146.  
  147.                                                                 $this->debugLevel = 1;  
  148.  
  149.                                
  150.  
  151.                             }
  152.  
  153.                        
  154.  
  155.                         //connection starts here
  156.  
  157.                 $this->connection = @mysql_connect($this->db_host.':'.$this->db_port, $this->db_user, $this->db_password);
  158.  
  159.                
  160.  
  161.                     if($this->connection)
  162.  
  163.                     {                              
  164.  
  165.                         $this->database = @mysql_select_db ($this->db_database);
  166.  
  167.                     }
  168.  
  169.  
  170.  
  171.                                 if(!$this->connection)
  172.  
  173.                                         {
  174.  
  175.                                         $this->throwError($this->exit);
  176.  
  177.                                                 return FALSE;
  178.  
  179.                                         }
  180.  
  181.                                                 else
  182.  
  183.                                                         return TRUE;
  184.  
  185.                            
  186.  
  187.                                         if(!$this->database)
  188.  
  189.                                                 {
  190.  
  191.                                                         $this->throwError($this->exit);
  192.  
  193.                                                         return FALSE;
  194.  
  195.                                                 }
  196.  
  197.                                                         else
  198.  
  199.                                                                 return TRUE;
  200.  
  201.  
  202.  
  203.                         }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.       /*
  210.  
  211.         Function throwing error.
  212.  
  213.         Also if you want to terminate your script after an error, change $exit value to (1) at the top of the class
  214.  
  215.       */
  216.  
  217.          
  218.  
  219.         public function throwError($exit)
  220.  
  221.                 {
  222.  
  223.                        
  224.  
  225.                 if(mysql_error() != NULL)
  226.  
  227.                 {
  228.  
  229.                        
  230.  
  231.                        
  232.  
  233.                         $_SESSION['error_env'] = $_SERVER['SERVER_NAME'];
  234.  
  235.                         $_SESSION['error_script'] =  $_SERVER['PHP_SELF'];
  236.  
  237.                         $_SESSION['error_sdb'] = __FILE__.':'.__LINE__;
  238.  
  239.                         $_SESSION['error_user'] = $this->db_user;
  240.  
  241.                         $_SESSION['error_time'] = date("j-m-Y, H:i:s");
  242.  
  243.                         $_SESSION['error_num'] = mysql_errno();
  244.  
  245.                         $_SESSION['error_syn'] = mysql_error();
  246.  
  247.                         $_SESSION['output_backtrace'] = $this->parseBacktrace(debug_backtrace());
  248.  
  249.  
  250.  
  251.                         echo $this->error ='MySQL Error #'.mysql_errno().' Syntax: '.mysql_error().'<br>';
  252.  
  253.                                 $this->errors++;
  254.  
  255.  
  256.  
  257.                                 if($this->exit == 1)
  258.  
  259.                                         {
  260.  
  261.                                         echo 'Application terminated';
  262.  
  263.                                         exit();
  264.  
  265.                                         }
  266.  
  267.                 }
  268.  
  269.  
  270.  
  271.  
  272.  
  273.                 }
  274.  
  275.         /*
  276.  
  277.         This function throws full backtrace if error occurs.
  278.  
  279.         Fixed displaying connection details like host, password...
  280.  
  281.         */
  282.  
  283.                
  284.  
  285.         public function parseBacktrace($raw)
  286.  
  287.                 {
  288.  
  289.                         $forbidden = array(1045, 2003);
  290.  
  291.        
  292.  
  293.         foreach($raw as $entry){
  294.  
  295.                 $this->backtrace.="File: ".$entry['file']." (Line: ".$entry['line'].")<br>";
  296.  
  297.                 $this->backtrace.="Function: ".$entry['function']."<br>";
  298.  
  299.  
  300.  
  301.                 if(!in_array($_SESSION['error_num'], $forbidden))  //why would we parse login or password data? :)
  302.  
  303.                 {
  304.  
  305.                 $this->backtrace.="Args: ".implode(", ", $entry['args'])."<br>";
  306.  
  307.                 }
  308.  
  309.         }
  310.  
  311.  
  312.  
  313.         return $this->backtrace;
  314.  
  315.                                        
  316.  
  317.                 }
  318.  
  319.  
  320.  
  321.         /*
  322.  
  323.         Make query to database
  324.  
  325.         Default - with result
  326.  
  327.         $resource = 0 - only execution, no results avaible
  328.  
  329.         */            
  330.  
  331.  
  332.  
  333.         public function query($syntax, $resource = NULL)
  334.  
  335.                
  336.  
  337.                 {
  338.  
  339.                         if($this->connection)
  340.  
  341.                         {
  342.  
  343.                         $this->syntax = $syntax;
  344.  
  345.                         if(!isset($resource))
  346.  
  347.                         {
  348.  
  349.                         $this->resource = 1;
  350.  
  351.                         }
  352.  
  353.                                 else
  354.  
  355.                                 {
  356.  
  357.                                 $this->resource = $resource;
  358.  
  359.                                 }
  360.  
  361.  
  362.  
  363.                         if($this->debugLevel == 2)
  364.  
  365.                                 {
  366.  
  367.                                 $start = $this->getTime();
  368.  
  369.                                 }
  370.  
  371.                        
  372.  
  373.                         if($this->resource == 1)
  374.  
  375.                         {
  376.  
  377.                                 $this->result = @mysql_query($this->syntax);
  378.  
  379.                                
  380.  
  381.                                
  382.  
  383.                                 if(!$this->result)
  384.  
  385.                                 {
  386.  
  387.                                 $this->throwError($this->exit);
  388.  
  389.                                 return FALSE;
  390.  
  391.                                 }
  392.  
  393.  
  394.  
  395.                         }
  396.  
  397.                                 else if($this->resource == 0)
  398.  
  399.                                 {
  400.  
  401.                                 $this->exe = @mysql_query($this->syntax);
  402.  
  403.                                
  404.  
  405.  
  406.  
  407.                                 if(!$this->result)
  408.  
  409.                                 {
  410.  
  411.                                 $this->throwError($this->exit);
  412.  
  413.                                 return FALSE;
  414.  
  415.                                 }
  416.  
  417.                                
  418.  
  419.                                 }
  420.  
  421.                                 if($this->result == TRUE || $this->exe == TRUE)
  422.  
  423.                                 {
  424.  
  425.                                         $this->queries++;
  426.  
  427.                                         if($this->debugLevel == 2)
  428.  
  429.                                                         {
  430.  
  431.                             $end = $this->getTime();
  432.  
  433.                                                         $this->syntaxes .= round($end-$start, 4).' sec. '.$this->syntax.'<br>';
  434.  
  435.                                                         }
  436.  
  437.                                 }
  438.  
  439.  
  440.  
  441.  
  442.  
  443.                                         if(isset($this->result))
  444.  
  445.                                                 return $this->result; //returning resource
  446.  
  447.  
  448.  
  449.                                                         else if(isset($this->exe))
  450.  
  451.                                                 return TRUE; //returning bool :)
  452.  
  453.  
  454.  
  455.  
  456.  
  457.                 }
  458.  
  459.                 }
  460.  
  461.  
  462.  
  463.        
  464.  
  465.  
  466.  
  467.         /*
  468.  
  469.         Fetch results from last query, you can choose mode
  470.  
  471.         1- MYSQL_BOTH
  472.  
  473.         2- MYSQL_ASSOC
  474.  
  475.         3- MYSQL_NUM
  476.  
  477.         */
  478.  
  479.         public function fetch($result = NULL, $mode = NULL)
  480.  
  481.                 {
  482.  
  483.                                 if($this->result && $this->connection)
  484.  
  485.                                         {
  486.  
  487.                         $this->mode = $mode;
  488.  
  489.                        
  490.  
  491.                         switch ($mode)
  492.  
  493.                             {
  494.  
  495.                               case 1:
  496.  
  497.                               $this->mode = MYSQL_BOTH;
  498.  
  499.                               break;
  500.  
  501.                              
  502.  
  503.                               case 2:
  504.  
  505.                               $this->mode = MYSQL_ASSOC;
  506.  
  507.                               break;
  508.  
  509.                              
  510.  
  511.                               case 3:
  512.  
  513.                               $this->mode = MYSQL_NUM;
  514.  
  515.                               break;
  516.  
  517.                              
  518.  
  519.                                 default:
  520.  
  521.                                 $this->mode = MYSQL_BOTH;  
  522.  
  523.                                
  524.  
  525.                             }
  526.  
  527.                                
  528.  
  529.                                 if(isset($result)) //if you want to choose other than last result
  530.  
  531.                                                         //but you have to make query like this: $q = $DB->query(...)
  532.  
  533.                                         {
  534.  
  535.                                                 $this->result = $result;
  536.  
  537.  
  538.  
  539.                                                 }
  540.  
  541.  
  542.  
  543.                         $this->fetched = @mysql_fetch_array($this->result, $this->mode);
  544.  
  545.  
  546.  
  547.                                 if(is_array($this->fetched))
  548.  
  549.                                         {
  550.  
  551.                                         return $this->fetched;
  552.  
  553.                                         }
  554.  
  555.                                         else
  556.  
  557.                                         {
  558.  
  559.                                         $this->throwError($this->exit);
  560.  
  561.                                         return FALSE;
  562.  
  563.                                         }
  564.  
  565.  
  566.  
  567.                                         }
  568.  
  569.                 }
  570.  
  571.  
  572.  
  573.                
  574.  
  575.  
  576.  
  577.        /*
  578.  
  579.       Create new database with setted name and charset, throwing an error while not sufficient access
  580.  
  581.       */
  582.  
  583.         public function createDB($name, $charset)
  584.  
  585.                 {
  586.  
  587.                         if($this->connection)
  588.  
  589.                                 {
  590.  
  591.                                 $this->creator = $this->query('CREATE DATABASE '.$name.' DEFAULT CHARACTER SET '.$charset.'');
  592.  
  593.  
  594.  
  595.                         if($this->creator)
  596.  
  597.                                 {
  598.  
  599.                                 return TRUE;
  600.  
  601.                                 }
  602.  
  603.  
  604.  
  605.  
  606.  
  607.                                 }
  608.  
  609.        
  610.  
  611.                 }
  612.  
  613.            
  614.  
  615.     /*
  616.  
  617.     Lock tables (WRITE) table will be locked from WRITE access, READ access allowed
  618.  
  619.     */  
  620.  
  621.  
  622.  
  623.     public function LockTableWrite()
  624.  
  625.         {
  626.  
  627.             if($this->connection)
  628.  
  629.             {
  630.  
  631.             foreach (func_get_args() as $tablename)
  632.  
  633.             {
  634.  
  635.                $this->lockedWrite = $this->query('LOCK TABLES '.$tablename.' READ',0); //dont look at arg. READ it will lock WRITING in :)
  636.  
  637.  
  638.  
  639.                     }
  640.  
  641.                         if($this->lockedWrite)
  642.  
  643.                      return TRUE;
  644.  
  645.                            
  646.  
  647.             }
  648.  
  649.         }
  650.  
  651.        
  652.  
  653.      /*
  654.  
  655.     Lock tables (READ) table will be locked from READ and WRITE access
  656.  
  657.     */  
  658.  
  659.  
  660.  
  661.     public function LockTableRead()
  662.  
  663.         {
  664.  
  665.             if($this->connection)
  666.  
  667.             {
  668.  
  669.             foreach (func_get_args() as $tablename)
  670.  
  671.             {
  672.  
  673.                 $this->lockedRead = $this->query('LOCK TABLES '.$tablename.' WRITE',0);
  674.  
  675.                
  676.  
  677.                     }
  678.  
  679.                         if($this->lockedRead)
  680.  
  681.                      return TRUE;  
  682.  
  683.             }
  684.  
  685.         }
  686.  
  687.        
  688.  
  689.     /*
  690.  
  691.     Unlock tables locked before
  692.  
  693.     */  
  694.  
  695.  
  696.  
  697.     public function Unlock()
  698.  
  699.         {
  700.  
  701.             if($this->connection)
  702.  
  703.             {
  704.  
  705.             $this->unlock = $this->query('UNLOCK TABLES',0);
  706.  
  707.             }
  708.  
  709.                 if($this->unlock)
  710.  
  711.                     {
  712.  
  713.                      return TRUE;  
  714.  
  715.                     }
  716.  
  717.         }
  718.  
  719.  
  720.  
  721.            
  722.  
  723.            
  724.  
  725.     /*
  726.  
  727.     Optimize database
  728.  
  729.     */
  730.  
  731.    
  732.  
  733.     public function optimizeDB($output = NULL)
  734.  
  735.    
  736.  
  737.         {
  738.  
  739.             if($this->connection)
  740.  
  741.             {
  742.  
  743.           $this->query('SHOW TABLES');
  744.  
  745.  
  746.  
  747.                         while($table = $this->fetch(NULL,2))
  748.  
  749.                                  {
  750.  
  751.                                  
  752.  
  753.                                 foreach ($table as $db)
  754.  
  755.                                 {
  756.  
  757.                
  758.  
  759.                 $this->query('OPTIMIZE TABLE '.$db.'', 0);
  760.  
  761.             if($output == TRUE)
  762.  
  763.                 {
  764.  
  765.                                 echo $db.' - Optimized<br>';
  766.  
  767.                    
  768.  
  769.                 }
  770.  
  771.            
  772.  
  773.                                 }
  774.  
  775.    
  776.  
  777.                                 }
  778.  
  779.             }
  780.  
  781.            
  782.  
  783.         }
  784.  
  785.  
  786.  
  787.            
  788.  
  789.     /*
  790.  
  791.     Clear (truncate) table from records
  792.  
  793.     */
  794.  
  795.     public function clearTable($table)
  796.  
  797.         {
  798.  
  799.             if($this->connection)
  800.  
  801.                 {
  802.  
  803.                  $this->clear = $this->query('TRUNCATE TABLE '.$table.'',0);
  804.  
  805.                     if($this->clear)
  806.  
  807.                         {
  808.  
  809.                           return TRUE;  
  810.  
  811.                         }
  812.  
  813.                        
  814.  
  815.                 }  
  816.  
  817.  
  818.  
  819.         }
  820.  
  821.  
  822.  
  823.        
  824.  
  825.         /*
  826.  
  827.         DEPRACETED
  828.  
  829.         Import dump using exec function, u have to be logged to mysql admin user
  830.  
  831.         Works only on Unix like OS.
  832.  
  833.        
  834.  
  835.         public function importDumpexec($location)
  836.  
  837.                         {
  838.  
  839.                                 if($this->connection)
  840.  
  841.                                         {
  842.  
  843.                                                 if(file_exists($location) && function_exists('exec'))
  844.  
  845.                                                 {
  846.  
  847.                                                         //check if superuser!
  848.  
  849.  
  850.  
  851.                                                 $cmd = '/usr/bin/mysql -h '.$this->db_host.' -u '.$this->db_user.' -p'.$this->db_password.' '.$this->db_database.' < '.$location;
  852.  
  853.                                                 $this->dump = exec($cmd, $result);
  854.  
  855.        
  856.  
  857.                                                 if($this->dump == 0)
  858.  
  859.                                                         {
  860.  
  861.                                                                 //echo 'Import resulted an error...';
  862.  
  863.                                                                 return FALSE;
  864.  
  865.                                                         }
  866.  
  867.  
  868.  
  869.                                                 }
  870.  
  871.                                                 else
  872.  
  873.                                                         {
  874.  
  875.                                                         //echo 'Import success!';
  876.  
  877.                                                         return TRUE;
  878.  
  879.  
  880.  
  881.                                                         }
  882.  
  883.  
  884.  
  885.                                         }
  886.  
  887.  
  888.  
  889.                         }
  890.  
  891.                        
  892.  
  893.                         */
  894.  
  895.  
  896.  
  897.         /*
  898.  
  899.         FORCE disconnect from mysql. Killing connection.
  900.  
  901.         */
  902.  
  903.         public function disconnect()
  904.  
  905.                 {
  906.  
  907.                         $this->disconnect = @mysql_close($this->connection);
  908.  
  909.  
  910.  
  911.                                         unset($this->connection);
  912.  
  913.                                         unset($this->db_host);
  914.  
  915.                                         unset($this->db_login);
  916.  
  917.                                         unset($this->db_password);
  918.  
  919.                                         unset($this->db_database);
  920.  
  921.                                         unset($this->db_port);
  922.  
  923.  
  924.  
  925.                                 if($this->disconnect)
  926.  
  927.                                         {
  928.  
  929.                                                 if($this->disconnect && $this->debugLevel == 2)
  930.  
  931.                                                 {
  932.  
  933.                                                 echo 'Disconnected';
  934.  
  935.                                                 return TRUE;
  936.  
  937.                                                 }
  938.  
  939.                                                 return TRUE;
  940.  
  941.                                                 }
  942.  
  943.                                         else
  944.  
  945.                                         {
  946.  
  947.                                 $this->throwError($this->exit);
  948.  
  949.                                 return FALSE;
  950.  
  951.                                         }
  952.  
  953.  
  954.  
  955.  
  956.  
  957.                 }
  958.  
  959.  
  960.  
  961.        
  962.  
  963.       /*
  964.  
  965.       List mysql variables such as client encoding and version
  966.  
  967.       */  
  968.  
  969.  
  970.  
  971.     public function dbVars()
  972.  
  973.         {
  974.  
  975.            if($this->connection)
  976.  
  977.                 {
  978.  
  979.                   $this->vars .= 'Client Encoding: '.mysql_client_encoding($this->connection).'</br>';
  980.  
  981.                         $this->vars .= 'Server Version: '.mysql_get_server_info().'</br>';
  982.  
  983.  
  984.  
  985.                                 return $this->vars;
  986.  
  987.                 }
  988.  
  989.  
  990.  
  991.         }
  992.  
  993.       /*
  994.  
  995.       Show mysql statistics like queries per second, long queries, uptime and so one
  996.  
  997.       */
  998.  
  999.  
  1000.  
  1001.     public function dbStatistics()
  1002.  
  1003.         {
  1004.  
  1005.          if($this->connection)
  1006.  
  1007.             {
  1008.  
  1009.             $this->statistics = mysql_stat($this->connection);
  1010.  
  1011.                 return $this->statistics;
  1012.  
  1013.  
  1014.  
  1015.             }  
  1016.  
  1017.  
  1018.  
  1019.         }
  1020.  
  1021.  
  1022.  
  1023.         /*
  1024.  
  1025.         Returns affected rows, results made by query.
  1026.  
  1027.         You can choose query, from which the number of results will be displayed
  1028.  
  1029.         */
  1030.  
  1031.  
  1032.  
  1033.         public function numRows($res = NULL)
  1034.  
  1035.                 {
  1036.  
  1037.                         if($this->connection)
  1038.  
  1039.                         {
  1040.  
  1041.                                 if(isset($res))
  1042.  
  1043.                                 {
  1044.  
  1045.                                 $this->result = $res;
  1046.  
  1047.                                 $this->rows = @mysql_num_rows($this->result);
  1048.  
  1049.                                 }
  1050.  
  1051.                                         else
  1052.  
  1053.                                                 {
  1054.  
  1055.                                                         if(isset($this->result))
  1056.  
  1057.                                                         $this->rows = @mysql_num_rows($this->result);
  1058.  
  1059.  
  1060.  
  1061.                                                 }
  1062.  
  1063.  
  1064.  
  1065.                                 if($this->rows)
  1066.  
  1067.                                 return (int)$this->rows;
  1068.  
  1069.                                 else
  1070.  
  1071.                                 {
  1072.  
  1073.                                 $this->throwError($this->exit);
  1074.  
  1075.                                 return 0;
  1076.  
  1077.                                 }
  1078.  
  1079.                         }
  1080.  
  1081.                 }
  1082.  
  1083.  
  1084.  
  1085.         /*
  1086.  
  1087.         Show all queries syntaxes during script work
  1088.  
  1089.         */
  1090.  
  1091.  
  1092.  
  1093.     public function showSyntaxes()
  1094.  
  1095.                 {
  1096.  
  1097.                         if($this->queries > 0)
  1098.  
  1099.                         {
  1100.  
  1101.                 if($this->debugLevel == 2)
  1102.  
  1103.                 {
  1104.  
  1105.                
  1106.  
  1107.                                 return $this->syntaxes;
  1108.  
  1109.                        
  1110.  
  1111.                         }
  1112.  
  1113.                                 else
  1114.  
  1115.                                 echo 'Debug mode must be ENABLED (2) to use this function';
  1116.  
  1117.         }
  1118.  
  1119.         //no queries
  1120.  
  1121.  
  1122.  
  1123.                 }
  1124.  
  1125.        
  1126.  
  1127.      /*
  1128.  
  1129.      Counts time needed to finish query
  1130.  
  1131.      */  
  1132.  
  1133.        
  1134.  
  1135.     public function getTime()
  1136.  
  1137.         {
  1138.  
  1139.            
  1140.  
  1141.          static $a;
  1142.  
  1143.     if($a == 0) $a = microtime(true);
  1144.  
  1145.     else return (string)(microtime(true)-$a);  
  1146.  
  1147.            
  1148.  
  1149.            
  1150.  
  1151.         }
  1152.  
  1153.  
  1154.  
  1155.      /*
  1156.  
  1157.       Show last errors
  1158.  
  1159.       */
  1160.  
  1161.    
  1162.  
  1163.         public function showError()
  1164.  
  1165.    
  1166.  
  1167.          {
  1168.  
  1169.         if($this->errors > 0)
  1170.  
  1171.             {
  1172.  
  1173.         return $this->error;
  1174.  
  1175.             }
  1176.  
  1177.             else
  1178.  
  1179.                 return 'No errors found';
  1180.  
  1181.  
  1182.  
  1183.          }
  1184.  
  1185.  
  1186.  
  1187.         /*
  1188.  
  1189.         Function to count number of errors
  1190.  
  1191.         */
  1192.  
  1193.  
  1194.  
  1195.         public function countErrors()
  1196.  
  1197.        
  1198.  
  1199.         {
  1200.  
  1201.  
  1202.  
  1203.         return $this->errors;
  1204.  
  1205.  
  1206.  
  1207.         }
  1208.  
  1209.  
  1210.  
  1211.         /*
  1212.  
  1213.         Show actual debug level
  1214.  
  1215.         */
  1216.  
  1217.        
  1218.  
  1219.         public function showDebugLevel()
  1220.  
  1221.        
  1222.  
  1223.         {
  1224.  
  1225.         return $this->debugLevel;
  1226.  
  1227.  
  1228.  
  1229.         }
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.     /*
  1236.  
  1237.       Close connections and unset all variables (automatically)
  1238.  
  1239.       */
  1240.  
  1241.  
  1242.  
  1243.         public function __destruct()
  1244.  
  1245.                                 {
  1246.  
  1247.                    
  1248.  
  1249.                                         if($this->connection)
  1250.  
  1251.                                                  {
  1252.  
  1253.                                                                                         $this->disconnect();
  1254.  
  1255.                                                                                         unset($this->connection);
  1256.  
  1257.                                                  }
  1258.  
  1259.                                                                
  1260.  
  1261.                                                 //free memory
  1262.  
  1263.                        
  1264.  
  1265.                                                 if(isset($this->database))
  1266.  
  1267.                                         unset($this->database);
  1268.  
  1269.                                                 if(isset($this->fetched))
  1270.  
  1271.                                         unset($this->fetched);
  1272.  
  1273.                                                 if(isset($this->error))
  1274.  
  1275.                     unset($this->error);
  1276.  
  1277.                                                 if(isset($this->db_host))
  1278.  
  1279.                                         unset($this->db_host);
  1280.  
  1281.                                                 if(isset($this->db_port))
  1282.  
  1283.                                         unset($this->db_port);
  1284.  
  1285.                                                 if(isset($this->db_user))
  1286.  
  1287.                                         unset($this->db_user);
  1288.  
  1289.                                                 if(isset($this->db_password))
  1290.  
  1291.                                         unset($this->db_password);
  1292.  
  1293.                                                 if(isset($this->database))
  1294.  
  1295.                                         unset($this->db_database);
  1296.  
  1297.  
  1298.  
  1299.                                        
  1300.  
  1301.  
  1302.  
  1303.                                 }
  1304.  
  1305.                        
  1306.  
  1307.         }
  1308.  
  1309.