Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Jul 4th, 2009 | Syntax: PHP | Size: 15.37 KB | Hits: 61 | Expires: Never
Copy text to clipboard
  1. <?php
  2.  
  3.  # Med venlig hilsen
  4. # Andreas Madsen
  5.  
  6. class MySQLc
  7. {
  8.  
  9.         /*** MySQLi class ***/
  10.         public $mysqli = null;
  11.        
  12.         /*** Udvikler tilstand ***/
  13.         private $DevError = false;
  14.  
  15.        
  16.         /* START MySQLc class
  17.          * $user : user / admin / other
  18.          * hvis $user == other, $host,$database,$username,$password skal sættes
  19.          * return void
  20.          */
  21.         public function __construct($user, $host=null, $database=null, $username=null, $password=null)
  22.         {
  23.                 switch($user)
  24.                 {
  25.                         case "user":
  26.                                  $host          = __SQL_Host;
  27.                                  $database      = __SQL_Database;
  28.                                  $username      = __SQLuser_Username;
  29.                                  $password      = __SQLuser_Password;
  30.                                 break;
  31.                         case "admin":
  32.                                  $host          = __SQL_Host;
  33.                                  $database      = __SQL_Database;
  34.                                  $username      = __SQLadmin_Username;
  35.                                  $password      = __SQLadmin_Password;
  36.                                 break;
  37.                         case "other":
  38.                                 break;
  39.                         default:
  40.                                  echo '<pre>';throw new Exception ('No user set ('.$user.')');echo '</pre>';
  41.                 }
  42.                 $this->connect($host, $username, $password, $database);
  43.         }
  44.        
  45.         /* Opretter forbindelse til MySQL
  46.          * sætter $this->mysqli til new MySQLi objekt
  47.          * return void
  48.          */
  49.         private function connect($host, $username, $password, $database)
  50.         {
  51.                 $this->close();
  52.                 $this->mysqli = new mysqli($host, $username, $password, $database);
  53.                         if($this->mysqli->connect_errno)
  54.                         {
  55.                                 $this->AddError("Connection could not be established - mysqli returned: ".$this->mysqli->connect_error);
  56.                         }
  57.         }
  58.        
  59.        
  60.         /*
  61.          * Udvikler tilstand
  62.          * $value : true / false
  63.          * return void
  64.          */
  65.         public function Dev($value)
  66.         {
  67.                 $this->DevError = $value;
  68.         }
  69.        
  70.        
  71.         /* Automatisk valg af MySQLi type
  72.          * $query : sql kommando
  73.          * return objekt (med for bindelse til dette objekt igennem $this)
  74.          */
  75.         public function query($query)
  76.         {
  77.                 $type = 'query';
  78.                 if ( preg_match("/\?/", $query) )
  79.                 {
  80.                         $type = 'prepare';
  81.                         if ( preg_match("/'\?'/", $query) )
  82.                         {
  83.                                 $type = 'query';
  84.                         }
  85.                 }
  86.                
  87.                 return new c_query($query, $this, $type);
  88.         }
  89.        
  90.         /* Manual valg af "type" (query)
  91.          * $query : sql kommando
  92.          * return objekt (med for bindelse til dette objekt igennem $this)
  93.          */
  94.         public function ManualQuery($query)
  95.         {
  96.                 return new c_query($query, $this, 'query');
  97.         }
  98.        
  99.         /* Manual valg af "type" (prepare)
  100.          * $query : sql kommando
  101.          * return objekt (med for bindelse til dette objekt igennem $this)
  102.          */
  103.         public function ManualPrepare($query)
  104.         {
  105.                 return new c_query($query, $this, 'prepare');
  106.         }
  107.        
  108.         /* Behandler fejlmeddelser
  109.          * $msg : MySQLi fejl meddelse
  110.          * $query : Query SQL kommando
  111.          * $dev : Dev mode true / false
  112.          */
  113.         public function AddError($msg, $query = false, $dev = false)
  114.         {
  115.                 if ($dev === false)
  116.                 {
  117.                         $dev = $this;
  118.                 }
  119.                 $devmode = $dev->DevError;
  120.                 $error  = "[ ---- \r\n";
  121.                 $error .= "Tid:    " . date("H:i:s - d/m/Y", time()) . "\r\n";
  122.                 $error .= "Fil:    " . $this->curPageURL() . "\r\n";
  123.                 $error .= "Besked: " . $msg . "\r\n";
  124.                 $error .= ($query === false ? "" : "Query:  " . $query . "\r\n");
  125.                 $error .= "  ---- ] \r\n";
  126.                 if ($devmode == true)
  127.                 {
  128.                         $this->pre($error);
  129.                 }
  130.                 $fil = fopen(__SITE_PATH . "/logs/mysql.txt", "r+");
  131.                 fwrite($fil, $error);
  132.                 fclose($fil);
  133.         }
  134.        
  135.         /* Finder nuverende URL/URI
  136.          * retturn URL / URL
  137.          */
  138.         private function curPageURL()
  139.         {
  140.                 $pageURL = 'http';
  141.                         if (isset($_SERVER["HTTPS"])) {
  142.                                 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  143.                         }
  144.                 $pageURL .= "://";
  145.                         if ($_SERVER["SERVER_PORT"] != "80") {
  146.                                 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  147.                         } else {
  148.                                 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  149.                         }
  150.                 return $pageURL;
  151.         }
  152.        
  153.         /* Udsrkiver <pre> med style
  154.          * return void;
  155.          */
  156.         public function pre($string)
  157.         {
  158.                 echo '<pre style="max-width:588px;max-height:100px;background-color:#BEFF56;overflow:auto;">' . $string . '</pre>';
  159.         }
  160.        
  161.         /* lukker MySQLi objekt
  162.          * return void;
  163.          */
  164.         public function close()
  165.         {
  166.                 if ($this->mysqli !== null)
  167.                 {
  168.                         $this->mysqli->close();
  169.                 }
  170.                 $this->mysqli = null;
  171.                 $this->DevError = null;
  172.                 unset($this);
  173.         }
  174.        
  175.         /* Ved destruct kør $this->close();
  176.          * return void
  177.          */
  178.         public function __destruct()
  179.         {
  180.                 if ($this->mysqli !== null)
  181.                 {
  182.                         $this->close();
  183.                 }
  184.         }
  185.        
  186. }
  187.  
  188.  
  189.  
  190.  
  191. class c_query
  192. {
  193.  
  194.         /*** Query class ***/
  195.         public $query = null;
  196.        
  197.        
  198.         /*** SQL kommando ***/
  199.         private $sql;
  200.        
  201.         /*** Query type (query / prepare) ***/
  202.         private $type;
  203.        
  204.         /*** MySQLi class ***/
  205.         private $mysqlc;
  206.        
  207.        
  208.         /*** array til bind_result ***/
  209.         private $bind_result_arr = false;
  210.        
  211.         /*** params array ***/
  212.         private $param = array();
  213.        
  214.        
  215.         /*** gem row array ***/
  216.         private $multiResult = false;
  217.        
  218.         /*** free_result On/Off ***/
  219.         private $free_result = false;  
  220.        
  221.         /*** encode type UTF/ISO ***/
  222.         private $encode = 'UTF';
  223.        
  224.         /*** angriv om execute() er kørt ***/
  225.         private $execute = false;
  226.        
  227.         /*** respons fra MySQL-Query ***/
  228.         private $respons;
  229.        
  230.        
  231.         /*** While Result ***/
  232.         private $whileResult_nr = 0;
  233.         private $whileResult_arr;
  234.         private $whileResult_first = true;
  235.        
  236.         /*
  237.          * START Query states igennem MySQLc objektet
  238.          * $query : sql kommando => $this->sql
  239.          * $mysqlc : Objektet som dette objekt blev kaldt fra => $this->mysqlc
  240.          * $type : query / prepare - angriver hvilke funktioner der skal bruges senere => $this->type
  241.          */
  242.         public function __construct($query, $mysqlc, $type)
  243.         {
  244.                 $this->sql = $query;
  245.                 $this->type = $type;
  246.                 $this->mysqlc = $mysqlc;
  247.                
  248.                 //Hvilken type respons
  249.                 $tmp = trim($this->sql);
  250.                 $tmp = split(" ", $tmp);
  251.                 $type = strtolower($tmp[0]);
  252.  
  253.                 switch ($type)
  254.                 {
  255.                         case "insert":
  256.                                 $this->_insert();
  257.                                 break;
  258.                         case "select":
  259.                                 $this->_select();
  260.                                 break;
  261.                         case "update":
  262.                                 $this->_update();
  263.                                 break;
  264.                         case "delete":
  265.                                 $this->_delete();
  266.                                 break;
  267.                         default:
  268.                                 $this->_query();
  269.                 }
  270.         }
  271.        
  272.        
  273.         /* Sætter encode typpe hvis nødvendigt
  274.          * $encode : ISO/UTF - hvis ISO vil data blive konventeret fra UTF-8 til ISO-8859-1 eller omvendt ved database kontakt
  275.          * return void
  276.          */
  277.         public function setEncode($encode)
  278.         {
  279.                 $this->encode = $encode;
  280.         }
  281.        
  282.         /* Retunere respons
  283.          * return $this->respons - dette kan være insert_id / num_rows / affected_rows
  284.          */
  285.         public function GetRespons()
  286.         {
  287.                 if ($this->type == 'prepare')
  288.                 {
  289.                         $this->execute();
  290.                 }
  291.                 return $this->respons;
  292.         }
  293.        
  294.        
  295.         /* Tilføjer param - Kun hvis $this->type == prepare
  296.          * $type : fx s / i
  297.          * $value : fx hallo / 10
  298.          * Gemmer data i array - $this->param
  299.          * return void
  300.          */
  301.         public function addparam ($type, $value)
  302.         {
  303.                 if ($this->type == 'prepare')
  304.                 {
  305.                         $this->param[] = array(
  306.                                                                         'type' => $type,
  307.                                                                         'value' => ($this->encode == 'ISO' ? mb_convert_encoding($value, "ISO-8859-1", "UTF-8") : $value)
  308.                                                                    );
  309.                 }
  310.                 else
  311.                 {
  312.                         echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
  313.                 }
  314.         }
  315.        
  316.         /* forbinder param til prepare
  317.          * henter data fra $this->param (sat med $this->addparam())
  318.          * return void
  319.          */
  320.         private function bind_param()
  321.         {
  322.                 if ($this->type == 'prepare')
  323.                 {
  324.                         $types = '';
  325.                         $params = array();
  326.  
  327.                         foreach ($this->param as $subarr)
  328.                         {
  329.                                 $types .= $subarr['type'];
  330.                                 $params[] = $subarr['value'];
  331.                         }
  332.                         unset($subarr);
  333.                         $this->param = array();
  334.                        
  335.                         $args = array();
  336.                         $args[] = $types;
  337.                        
  338.                         foreach ($params as $value)
  339.                         {
  340.                                 $args[] = $value;
  341.                         }
  342.                         unset($value);
  343.                        
  344.                         call_user_func_array(array($this->query, 'bind_param'), $args);
  345.                 }
  346.                 else
  347.                 {
  348.                         echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
  349.                 }
  350.         }
  351.        
  352.        
  353.         /* Kør SQL kommando - Kun hvis $this->type == prepare
  354.          * sætter også $this->respons hvis nødvendigt
  355.          * return void
  356.          */
  357.         public function execute()
  358.         {
  359.                 if ($this->execute === false)
  360.                 {
  361.                         if ($this->type == 'prepare')
  362.                         {
  363.                                 $this->bind_param();
  364.                                 $this->query->execute();
  365.                                 $this->execute = true;
  366.                                 switch ($this->respons)
  367.                                 {
  368.                                         case "insert_id":
  369.                                                  $this->respons = $this->mysqlc->mysqli->insert_id;
  370.                                                 break;
  371.                                         case "num_rows":
  372.                                                  $this->query->store_result();
  373.                                                  $this->respons = $this->query->num_rows;
  374.                                                 break;
  375.                                         case "affected_rows":
  376.                                                  $this->respons = $this->mysqlc->mysqli->affected_rows;
  377.                                                 break;
  378.                                         default:
  379.                                                  $this->respons = false;
  380.                                 }
  381.                         }
  382.                         else
  383.                         {
  384.                                 echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
  385.                         }
  386.                 }
  387.         }
  388.  
  389.         /* Hvis result2array skal bruges 2 gang
  390.          * PS: dette er et "hach"
  391.          * $value : true / false
  392.          * return void;
  393.          */
  394.         public function MultiResult($value)
  395.         {
  396.                 $this->multiResult = $value;
  397.         }
  398.        
  399.         /* gemmmer database resultat i array
  400.          * return Multi dimensional array ($result)
  401.          */
  402.         public function result2array()
  403.         {
  404.                 global $row;
  405.                 if (is_array($this->multiResult) == true)
  406.                 {
  407.                         return $this->multiResult;
  408.                 }
  409.                
  410.                 if ($this->type == 'prepare')
  411.                 {
  412.                         $this->execute();
  413.                         if ($this->free_result == false)
  414.                         {
  415.                                 $this->bind_result();
  416.                         }
  417.                        
  418.                         $result = array();
  419.                         while ($this->query->fetch()) {
  420.                                 foreach($row as $key => $val)
  421.                                 {
  422.                                         $c[$key] = ($this->encode == 'ISO' ? mb_convert_encoding($val, "UTF-8", "ISO-8859-1") : $val);
  423.                                 }
  424.                                 unset($key,$val);
  425.                                 $result[] = $c;
  426.                         }
  427.                        
  428.                 }
  429.                 elseif ($this->type == 'query')
  430.                 {
  431.                         $result = array();
  432.                         while ($row = $this->query->fetch_assoc()) {
  433.                                 foreach($row as $key => $val)
  434.                                 {
  435.                                         $c[$key] = ($this->encode == 'ISO' ? mb_convert_encoding($val, "UTF-8", "ISO-8859-1") : $val);
  436.                                 }
  437.                                 unset($key,$val);
  438.                                 $result[] = $c;
  439.                         }
  440.                 }
  441.                
  442.                 if ($this->multiResult == true)
  443.                 {
  444.                         $this->multiResult = $result;
  445.                 }
  446.                 return $result;
  447.         }
  448.        
  449.         /* Køre result i while
  450.          * return true / false
  451.          */
  452.         public function whileResult(&$var_name)
  453.         {
  454.                 $return = false;
  455.                
  456.                 if ($this->whileResult_first == true)
  457.                 {
  458.                         $this->whileResult_first = false;
  459.                         $this->whileResult_arr = $this->result2array();
  460.                 }
  461.                
  462.                 if (isset($this->whileResult_arr[$this->whileResult_nr]) == true)
  463.                 {
  464.                         if (empty($this->whileResult_arr[$this->whileResult_nr]) != true)
  465.                         {
  466.                                 $var_name = $this->whileResult_arr[$this->whileResult_nr];
  467.                                 $return = true;
  468.                         }
  469.                 }
  470.                
  471.                 $this->whileResult_nr++;
  472.                
  473.                 return $return;
  474.         }
  475.        
  476.        
  477.         /* Gemmer resulat i global array
  478.          * udfør automatisk
  479.          */
  480.         private function bind_result()
  481.         {
  482.                 global $row;
  483.                 $params = array();
  484.                 $this->free_result = true;
  485.                 if ($this->bind_result_arr === false)
  486.                 {
  487.                         if ($this->type == 'prepare')
  488.                         {
  489.                                 $tmp = trim($this->sql);
  490.                                 $tmp = split(" ", $tmp);
  491.                                 $type = strtolower($tmp[0]);
  492.                                 if ($type == 'select')
  493.                                 {
  494.                                     $meta = $this->query->result_metadata();
  495.                                         $list = array();
  496.                                         while ($field = $meta->fetch_field())
  497.                                         {
  498.                                                 $list[] = $field->name;
  499.                                         }
  500.                                         $str  = '$this->query->bind_result('  . '$row[\'' . implode('\'], $row[\'', $list) . '\']' . ');';
  501.                                         eval($str);
  502.                                        
  503.                                         /*
  504.                                     $meta = $this->query->result_metadata();
  505.                                         while ($field = $meta->fetch_field())
  506.                                         {
  507.                                                 $params[] = &$$name[$field->name];
  508.                                         }
  509.                                         call_user_func_array(array($this->query, 'bind_result'), $params);
  510.                                         */
  511.                                 }
  512.                                 else
  513.                                 {
  514.                                         echo '<pre>';throw new Exception ('Dette er ikke en "select" SQL. (type: '.$type.')');echo '</pre>';
  515.                                 }
  516.                         }
  517.                         else
  518.                         {
  519.                                 echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
  520.                         }
  521.                 }
  522.                 else
  523.                 {
  524.                         $str  = '$this->query->bind_result('  . '$' . $name . '[\'' . implode('\'], $'. $name . '[\'', $this->bind_result_arr) . '\']' . ');';
  525.                         eval($str);
  526.                 }
  527.         }
  528.        
  529.        
  530.         /* Query / Prepare function (INSERT)
  531.          * return void
  532.          */
  533.         private function _insert()
  534.         {
  535.                 if ($this->type == 'query')
  536.                 {
  537.                         if($this->query = $this->mysqlc->mysqli->query($this->sql))
  538.                         {
  539.                                 $this->respons = $this->mysqlc->mysqli->insert_id;
  540.                         }
  541.                         else
  542.                         {
  543.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  544.                         }
  545.                 }
  546.                 elseif ($this->type == 'prepare')
  547.                 {
  548.                         if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
  549.                         {
  550.                                 $this->respons = 'insert_id';
  551.                         }
  552.                         else
  553.                         {
  554.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  555.                         }
  556.                 }
  557.         }
  558.        
  559.         /* Query / Prepare function (SELECT)
  560.          * return void
  561.          */
  562.         private function _select()
  563.         {
  564.                 if ($this->type == 'query')
  565.                 {
  566.                         if($this->query = $this->mysqlc->mysqli->query($this->sql))
  567.                         {
  568.                                 $this->respons = $this->query->num_rows;
  569.                         }
  570.                         else
  571.                         {
  572.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  573.                         }
  574.                 }
  575.                 elseif ($this->type == 'prepare')
  576.                 {
  577.                         if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
  578.                         {
  579.                                 $this->respons = 'num_rows';
  580.                         }
  581.                         else
  582.                         {
  583.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  584.                         }
  585.                 }
  586.         }
  587.        
  588.         /* Query / Prepare function (UPDATE)
  589.          * return void
  590.          */
  591.         private function _update()
  592.         {
  593.                 if ($this->type == 'query')
  594.                 {
  595.                         if($this->query = $this->mysqlc->mysqli->query($this->sql))
  596.                         {
  597.                                 $this->respons = $this->mysqlc->mysqli->affected_rows;
  598.                         }
  599.                         else
  600.                         {
  601.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  602.                         }
  603.                 }
  604.                 elseif ($this->type == 'prepare')
  605.                 {
  606.                         if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
  607.                         {
  608.                                 $this->respons = 'affected_rows';
  609.                         }
  610.                         else
  611.                         {
  612.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  613.                         }
  614.                 }
  615.         }
  616.        
  617.         /* Query / Prepare function (DELETE)
  618.          * return void
  619.          */
  620.         private function _delete()
  621.         {
  622.                 if ($this->type == 'query')
  623.                 {
  624.                         if($this->query = $this->mysqlc->mysqli->query($this->sql))
  625.                         {
  626.                                 $this->respons = $this->mysqlc->mysqli->affected_rows;
  627.                         }
  628.                         else
  629.                         {
  630.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  631.                         }
  632.                 }
  633.                 elseif ($this->type == 'prepare')
  634.                 {
  635.                         if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
  636.                         {
  637.                                 $this->respons = 'affected_rows';
  638.                         }
  639.                         else
  640.                         {
  641.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  642.                         }
  643.                 }
  644.         }
  645.        
  646.         /* Query / Prepare function (OTHER QUERY)
  647.          * return void
  648.          */
  649.         private function _query()
  650.         {
  651.                 if ($this->type == 'query')
  652.                 {
  653.                         if($this->query = $this->mysqlc->mysqli->query($this->sql))
  654.                         {
  655.                                 $this->respons = false;
  656.                         }
  657.                         else
  658.                         {
  659.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  660.                         }
  661.                 }
  662.                 elseif ($this->type == 'prepare')
  663.                 {
  664.                         if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
  665.                         {
  666.                                 $this->respons = false;
  667.                         }
  668.                         else
  669.                         {
  670.                                 $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
  671.                         }
  672.                 }
  673.         }
  674.        
  675.         /* lukker Query/Prepare objekt
  676.          * return void;
  677.          */
  678.         public function close()
  679.         {
  680.                 if ($this->free_result == true)
  681.                 {
  682.                         $this->query->free_result();
  683.                 }
  684.                 if ($this->query !== null)
  685.                 {
  686.                         $this->query->close();
  687.                 }
  688.                
  689.                 $this->query = null;
  690.                 $this->sql = null;
  691.                 $this->type = null;
  692.                 $this->mysqlc = null;
  693.                 $this->free_result = null;
  694.                 $this->respons = null;
  695.                 $this->param = null;
  696.                 $this->encode = null;
  697.                 unset($this);
  698.         }
  699.        
  700.         /* Ved destruct kør $this->close();
  701.          * return void
  702.          */
  703.         public function __destruct()
  704.         {
  705.                 if ($this->query !== null)
  706.                 {
  707.                         $this->close();
  708.                 }
  709.         }
  710.        
  711. }
  712.  
  713. ?>