Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Low-Level-MySQL-API
- class mysql
- {
- private $con;
- private $server = 'localhost';
- private $user = '';
- private $password = '';
- private $db = '';
- function __construct()
- {
- $this->con = @new mysqli($this->server,$this->user,$this->password,$this->db);
- if (mysqli_connect_errno() == 0)
- return $this->con;
- else
- echo 'Could not connect to database ('.$this->db.'): ' .mysqli_connect_errno(). ' : ' .mysqli_connect_error();
- }
- function __destruct()
- {
- $this->con->close();
- }
- function Error_Handler($debug)
- {
- foreach($debug as $msg)
- {
- echo '<div style="background-color: #BB2222; border: 1px solid #000000;"><b>'.$msg['file'].'</b> | <b>[l.'.$msg['line'].']</b> : <b>ERROR</b> in function "<b>'.$msg['function'].'()"</b>.<br />Arguments: ';
- print_r($msg['args']);
- echo '</div>';
- }
- return 0;
- }
- function stmt_bind_assoc (&$stmt, &$out) //fetch_assoc
- {
- $data = mysqli_stmt_result_metadata($stmt);
- $fields = array();
- $out = array();
- $fields[0] = $stmt;
- $count = 1;
- while($field = mysqli_fetch_field($data))
- {
- $fields[$count] = &$out[$field->name];
- $count++;
- }
- call_user_func_array(mysqli_stmt_bind_result, $fields);
- }
- function EscapeString($string)
- {
- return $this->con->real_escape_string($string);
- }
- function DeleteTable($table){
- $data = "DROP TABLE `".$table."`";
- if ($query = $this->con->prepare($data))
- {
- $query->execute();
- return true;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function Update($table,$name,$value,$editname,$content)
- {
- $data = "UPDATE `".$table."` Set `".$editname."`=? WHERE `".$name."`=?".$searchval;
- if ($query = $this->con->prepare($data))
- {
- $query->bind_param( 'ss',$content,$value);
- $query->execute();
- if ($query->affected_rows > 0)
- return true;
- else
- return false;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function ReadTimestamp($table,$id,$name,$col="ID")
- {
- $data = "SELECT UNIX_TIMESTAMP(".$name.") FROM `".$table."` WHERE `".$col."`=?";
- if ($query = $this->con->prepare($data))
- {
- $query->bind_param( 's',$id);
- $query->execute();
- $query->bind_result($row);
- while ($query->fetch())
- return $row;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function ReadColFromTable($table,$name,$searchval="")
- {
- $data = "SELECT ".$name." FROM `".$table.'`'.$searchval;
- if ($query = $this->con->prepare($data))
- {
- $query->execute();
- $row = array();
- $this->stmt_bind_assoc($query, $row);
- $cols = array();//Zellen werden in numeriertem Array zurueckgegeben
- while ($query->fetch())
- foreach ($row as $key => $value)//schneller als array_values!
- array_push($cols,$value);
- return $cols;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function ReadArrayFromTable($table,$name,$value,$searchval="")
- {
- $data = "SELECT * FROM `".$table."` WHERE `".$name."`=?".$searchval;
- if ($query = $this->con->prepare($data))
- {
- $query->bind_param( 's',$value);
- $query->execute();
- $row = array();
- $this->stmt_bind_assoc($query, $row);
- while ($query->fetch())
- return $row;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function ReadFromTable($table,$name,$value,$retval,$searchval="")
- {
- $data = "SELECT ".$retval." FROM `".$table."` WHERE `".$name."`=?".$searchval;
- if ($query = $this->con->prepare($data))
- {
- $query->bind_param( 's',$value);
- $query->execute();
- $query->bind_result($content);
- while ($query->fetch())
- return $content;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function AddToTable($table,$rows,$values,$check = false)
- {
- if ($check) //Doppelte Datensätze suchen und aus Liste löschen
- For ($i = 0; $i < count($rows); $i++)
- if (ReadFromTable($name,$rows[$i],$values[$i],$rows[$i])) {unset($rows[$i]); unset($values[$i]);}
- $data = "INSERT INTO `".$table."` (";
- $i = 0;
- foreach ($rows as $key => $value)
- {
- if ($i != count($rows)-1) $data = $data.'`'.$value.'`,'; else $data = $data.'`'.$value.'`)';
- $i++;
- }
- $data = $data.' VALUES (';
- $i = 0;
- foreach ($values as $key => $retvalue)
- {
- if ($i != count($values)-1) $data = $data.'?,'; else $data = $data.'?)';
- $type = $type.'s';
- $i++;
- }
- if ($query = $this->con->prepare($data))
- {
- array_unshift($values,$type);
- //Bypass wrong behaviour of call_user_func_array()
- foreach($values as $key => $value) $tmp[$key] = &$values[$key];
- call_user_func_array(array($query,'bind_param'), $tmp);
- $query->execute();
- if ($query->affected_rows > 0)
- return true;
- else
- return false;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function DeleteFromTable($table,$name,$value,$searchval="")
- {
- $data = "DELETE FROM `".$table."` WHERE `".$name."`=?".$searchval;
- if ($query = $this->con->prepare($data))
- {
- $query->bind_param( 's',$value);
- $query->execute();
- if ($query->affected_rows > 0)
- return true;
- else
- return false;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function GetCount($table,$searchvalue ="")
- {
- $data = "SELECT COUNT(*) FROM `".$table.'`'.$searchvalue;
- if ($query = $this->con->prepare($data))
- {
- $query->execute();
- $query->bind_result($count);
- while ($query->fetch())
- return $count;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function ClearTable($table)
- {
- $data ="TRUNCATE TABLE `".$table.'`';
- if ($query = $this->con->prepare($data))
- {
- $query->execute();
- if (empty($query->error))
- return true;
- else return false;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- function ResetAutoIncrement($table)
- {
- $data ="ALTER TABLE `".$table."` AUTO_INCREMENT = 1";
- if ($query = $this->con->prepare($data))
- {
- $query->execute();
- if (empty($query->error))
- return true;
- else return false;
- }
- else
- return $this->Error_Handler(debug_backtrace());
- $query->close();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment