Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # Med venlig hilsen
- # Andreas Madsen
- class MySQLc
- {
- /*** MySQLi class ***/
- public $mysqli = null;
- /*** Udvikler tilstand ***/
- private $DevError = false;
- /* START MySQLc class
- * $user : user / admin / other
- * hvis $user == other, $host,$database,$username,$password skal sættes
- * return void
- */
- public function __construct($user, $host=null, $database=null, $username=null, $password=null)
- {
- switch($user)
- {
- case "user":
- $host = __SQL_Host;
- $database = __SQL_Database;
- $username = __SQLuser_Username;
- $password = __SQLuser_Password;
- break;
- case "admin":
- $host = __SQL_Host;
- $database = __SQL_Database;
- $username = __SQLadmin_Username;
- $password = __SQLadmin_Password;
- break;
- case "other":
- break;
- default:
- echo '<pre>';throw new Exception ('No user set ('.$user.')');echo '</pre>';
- }
- $this->connect($host, $username, $password, $database);
- }
- /* Opretter forbindelse til MySQL
- * sætter $this->mysqli til new MySQLi objekt
- * return void
- */
- private function connect($host, $username, $password, $database)
- {
- $this->close();
- $this->mysqli = new mysqli($host, $username, $password, $database);
- if($this->mysqli->connect_errno)
- {
- $this->AddError("Connection could not be established - mysqli returned: ".$this->mysqli->connect_error);
- }
- }
- /*
- * Udvikler tilstand
- * $value : true / false
- * return void
- */
- public function Dev($value)
- {
- $this->DevError = $value;
- }
- /* Automatisk valg af MySQLi type
- * $query : sql kommando
- * return objekt (med for bindelse til dette objekt igennem $this)
- */
- public function query($query)
- {
- $type = 'query';
- if ( preg_match("/\?/", $query) )
- {
- $type = 'prepare';
- if ( preg_match("/'\?'/", $query) )
- {
- $type = 'query';
- }
- }
- return new c_query($query, $this, $type);
- }
- /* Manual valg af "type" (query)
- * $query : sql kommando
- * return objekt (med for bindelse til dette objekt igennem $this)
- */
- public function ManualQuery($query)
- {
- return new c_query($query, $this, 'query');
- }
- /* Manual valg af "type" (prepare)
- * $query : sql kommando
- * return objekt (med for bindelse til dette objekt igennem $this)
- */
- public function ManualPrepare($query)
- {
- return new c_query($query, $this, 'prepare');
- }
- /* Behandler fejlmeddelser
- * $msg : MySQLi fejl meddelse
- * $query : Query SQL kommando
- * $dev : Dev mode true / false
- */
- public function AddError($msg, $query = false, $dev = false)
- {
- if ($dev === false)
- {
- $dev = $this;
- }
- $devmode = $dev->DevError;
- $error = "[ ---- \r\n";
- $error .= "Tid: " . date("H:i:s - d/m/Y", time()) . "\r\n";
- $error .= "Fil: " . $this->curPageURL() . "\r\n";
- $error .= "Besked: " . $msg . "\r\n";
- $error .= ($query === false ? "" : "Query: " . $query . "\r\n");
- $error .= " ---- ] \r\n";
- if ($devmode == true)
- {
- $this->pre($error);
- }
- $fil = fopen(__SITE_PATH . "/logs/mysql.txt", "r+");
- fwrite($fil, $error);
- fclose($fil);
- }
- /* Finder nuverende URL/URI
- * retturn URL / URL
- */
- private function curPageURL()
- {
- $pageURL = 'http';
- if (isset($_SERVER["HTTPS"])) {
- if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
- }
- $pageURL .= "://";
- if ($_SERVER["SERVER_PORT"] != "80") {
- $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
- } else {
- $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
- }
- return $pageURL;
- }
- /* Udsrkiver <pre> med style
- * return void;
- */
- public function pre($string)
- {
- echo '<pre style="max-width:588px;max-height:100px;background-color:#BEFF56;overflow:auto;">' . $string . '</pre>';
- }
- /* lukker MySQLi objekt
- * return void;
- */
- public function close()
- {
- if ($this->mysqli !== null)
- {
- $this->mysqli->close();
- }
- $this->mysqli = null;
- $this->DevError = null;
- unset($this);
- }
- /* Ved destruct kør $this->close();
- * return void
- */
- public function __destruct()
- {
- if ($this->mysqli !== null)
- {
- $this->close();
- }
- }
- }
- class c_query
- {
- /*** Query class ***/
- public $query = null;
- /*** SQL kommando ***/
- private $sql;
- /*** Query type (query / prepare) ***/
- private $type;
- /*** MySQLi class ***/
- private $mysqlc;
- /*** array til bind_result ***/
- private $bind_result_arr = false;
- /*** params array ***/
- private $param = array();
- /*** gem row array ***/
- private $multiResult = false;
- /*** free_result On/Off ***/
- private $free_result = false;
- /*** encode type UTF/ISO ***/
- private $encode = 'UTF';
- /*** angriv om execute() er kørt ***/
- private $execute = false;
- /*** respons fra MySQL-Query ***/
- private $respons;
- /*** While Result ***/
- private $whileResult_nr = 0;
- private $whileResult_arr;
- private $whileResult_first = true;
- /*
- * START Query states igennem MySQLc objektet
- * $query : sql kommando => $this->sql
- * $mysqlc : Objektet som dette objekt blev kaldt fra => $this->mysqlc
- * $type : query / prepare - angriver hvilke funktioner der skal bruges senere => $this->type
- */
- public function __construct($query, $mysqlc, $type)
- {
- $this->sql = $query;
- $this->type = $type;
- $this->mysqlc = $mysqlc;
- //Hvilken type respons
- $tmp = trim($this->sql);
- $tmp = split(" ", $tmp);
- $type = strtolower($tmp[0]);
- switch ($type)
- {
- case "insert":
- $this->_insert();
- break;
- case "select":
- $this->_select();
- break;
- case "update":
- $this->_update();
- break;
- case "delete":
- $this->_delete();
- break;
- default:
- $this->_query();
- }
- }
- /* Sætter encode typpe hvis nødvendigt
- * $encode : ISO/UTF - hvis ISO vil data blive konventeret fra UTF-8 til ISO-8859-1 eller omvendt ved database kontakt
- * return void
- */
- public function setEncode($encode)
- {
- $this->encode = $encode;
- }
- /* Retunere respons
- * return $this->respons - dette kan være insert_id / num_rows / affected_rows
- */
- public function GetRespons()
- {
- if ($this->type == 'prepare')
- {
- $this->execute();
- }
- return $this->respons;
- }
- /* Tilføjer param - Kun hvis $this->type == prepare
- * $type : fx s / i
- * $value : fx hallo / 10
- * Gemmer data i array - $this->param
- * return void
- */
- public function addparam ($type, $value)
- {
- if ($this->type == 'prepare')
- {
- $this->param[] = array(
- 'type' => $type,
- 'value' => ($this->encode == 'ISO' ? mb_convert_encoding($value, "ISO-8859-1", "UTF-8") : $value)
- );
- }
- else
- {
- echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
- }
- }
- /* forbinder param til prepare
- * henter data fra $this->param (sat med $this->addparam())
- * return void
- */
- private function bind_param()
- {
- if ($this->type == 'prepare')
- {
- $types = '';
- $params = array();
- foreach ($this->param as $subarr)
- {
- $types .= $subarr['type'];
- $params[] = $subarr['value'];
- }
- unset($subarr);
- $this->param = array();
- $args = array();
- $args[] = $types;
- foreach ($params as $value)
- {
- $args[] = $value;
- }
- unset($value);
- call_user_func_array(array($this->query, 'bind_param'), $args);
- }
- else
- {
- echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
- }
- }
- /* Kør SQL kommando - Kun hvis $this->type == prepare
- * sætter også $this->respons hvis nødvendigt
- * return void
- */
- public function execute()
- {
- if ($this->execute === false)
- {
- if ($this->type == 'prepare')
- {
- $this->bind_param();
- $this->query->execute();
- $this->execute = true;
- switch ($this->respons)
- {
- case "insert_id":
- $this->respons = $this->mysqlc->mysqli->insert_id;
- break;
- case "num_rows":
- $this->query->store_result();
- $this->respons = $this->query->num_rows;
- break;
- case "affected_rows":
- $this->respons = $this->mysqlc->mysqli->affected_rows;
- break;
- default:
- $this->respons = false;
- }
- }
- else
- {
- echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
- }
- }
- }
- /* Hvis result2array skal bruges 2 gang
- * PS: dette er et "hach"
- * $value : true / false
- * return void;
- */
- public function MultiResult($value)
- {
- $this->multiResult = $value;
- }
- /* gemmmer database resultat i array
- * return Multi dimensional array ($result)
- */
- public function result2array()
- {
- global $row;
- if (is_array($this->multiResult) == true)
- {
- return $this->multiResult;
- }
- if ($this->type == 'prepare')
- {
- $this->execute();
- if ($this->free_result == false)
- {
- $this->bind_result();
- }
- $result = array();
- while ($this->query->fetch()) {
- foreach($row as $key => $val)
- {
- $c[$key] = ($this->encode == 'ISO' ? mb_convert_encoding($val, "UTF-8", "ISO-8859-1") : $val);
- }
- unset($key,$val);
- $result[] = $c;
- }
- }
- elseif ($this->type == 'query')
- {
- $result = array();
- while ($row = $this->query->fetch_assoc()) {
- foreach($row as $key => $val)
- {
- $c[$key] = ($this->encode == 'ISO' ? mb_convert_encoding($val, "UTF-8", "ISO-8859-1") : $val);
- }
- unset($key,$val);
- $result[] = $c;
- }
- }
- if ($this->multiResult == true)
- {
- $this->multiResult = $result;
- }
- return $result;
- }
- /* Køre result i while
- * return true / false
- */
- public function whileResult(&$var_name)
- {
- $return = false;
- if ($this->whileResult_first == true)
- {
- $this->whileResult_first = false;
- $this->whileResult_arr = $this->result2array();
- }
- if (isset($this->whileResult_arr[$this->whileResult_nr]) == true)
- {
- if (empty($this->whileResult_arr[$this->whileResult_nr]) != true)
- {
- $var_name = $this->whileResult_arr[$this->whileResult_nr];
- $return = true;
- }
- }
- $this->whileResult_nr++;
- return $return;
- }
- /* Gemmer resulat i global array
- * udfør automatisk
- */
- private function bind_result()
- {
- global $row;
- $params = array();
- $this->free_result = true;
- if ($this->bind_result_arr === false)
- {
- if ($this->type == 'prepare')
- {
- $tmp = trim($this->sql);
- $tmp = split(" ", $tmp);
- $type = strtolower($tmp[0]);
- if ($type == 'select')
- {
- $meta = $this->query->result_metadata();
- $list = array();
- while ($field = $meta->fetch_field())
- {
- $list[] = $field->name;
- }
- $str = '$this->query->bind_result(' . '$row[\'' . implode('\'], $row[\'', $list) . '\']' . ');';
- eval($str);
- /*
- $meta = $this->query->result_metadata();
- while ($field = $meta->fetch_field())
- {
- $params[] = &$$name[$field->name];
- }
- call_user_func_array(array($this->query, 'bind_result'), $params);
- */
- }
- else
- {
- echo '<pre>';throw new Exception ('Dette er ikke en "select" SQL. (type: '.$type.')');echo '</pre>';
- }
- }
- else
- {
- echo '<pre>';throw new Exception ('Dette er ikke en "prepare". (type: '.$this->type.')');echo '</pre>';
- }
- }
- else
- {
- $str = '$this->query->bind_result(' . '$' . $name . '[\'' . implode('\'], $'. $name . '[\'', $this->bind_result_arr) . '\']' . ');';
- eval($str);
- }
- }
- /* Query / Prepare function (INSERT)
- * return void
- */
- private function _insert()
- {
- if ($this->type == 'query')
- {
- if($this->query = $this->mysqlc->mysqli->query($this->sql))
- {
- $this->respons = $this->mysqlc->mysqli->insert_id;
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- elseif ($this->type == 'prepare')
- {
- if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
- {
- $this->respons = 'insert_id';
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- }
- /* Query / Prepare function (SELECT)
- * return void
- */
- private function _select()
- {
- if ($this->type == 'query')
- {
- if($this->query = $this->mysqlc->mysqli->query($this->sql))
- {
- $this->respons = $this->query->num_rows;
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- elseif ($this->type == 'prepare')
- {
- if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
- {
- $this->respons = 'num_rows';
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- }
- /* Query / Prepare function (UPDATE)
- * return void
- */
- private function _update()
- {
- if ($this->type == 'query')
- {
- if($this->query = $this->mysqlc->mysqli->query($this->sql))
- {
- $this->respons = $this->mysqlc->mysqli->affected_rows;
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- elseif ($this->type == 'prepare')
- {
- if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
- {
- $this->respons = 'affected_rows';
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- }
- /* Query / Prepare function (DELETE)
- * return void
- */
- private function _delete()
- {
- if ($this->type == 'query')
- {
- if($this->query = $this->mysqlc->mysqli->query($this->sql))
- {
- $this->respons = $this->mysqlc->mysqli->affected_rows;
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- elseif ($this->type == 'prepare')
- {
- if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
- {
- $this->respons = 'affected_rows';
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- }
- /* Query / Prepare function (OTHER QUERY)
- * return void
- */
- private function _query()
- {
- if ($this->type == 'query')
- {
- if($this->query = $this->mysqlc->mysqli->query($this->sql))
- {
- $this->respons = false;
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- elseif ($this->type == 'prepare')
- {
- if($this->query = $this->mysqlc->mysqli->prepare($this->sql))
- {
- $this->respons = false;
- }
- else
- {
- $this->mysqlc->AddError($this->mysqlc->mysqli->error, $this->sql, $this->mysqlc);
- }
- }
- }
- /* lukker Query/Prepare objekt
- * return void;
- */
- public function close()
- {
- if ($this->free_result == true)
- {
- $this->query->free_result();
- }
- if ($this->query !== null)
- {
- $this->query->close();
- }
- $this->query = null;
- $this->sql = null;
- $this->type = null;
- $this->mysqlc = null;
- $this->free_result = null;
- $this->respons = null;
- $this->param = null;
- $this->encode = null;
- unset($this);
- }
- /* Ved destruct kør $this->close();
- * return void
- */
- public function __destruct()
- {
- if ($this->query !== null)
- {
- $this->close();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement