Added more debugging to SQL errors/warnings
This commit is contained in:
@@ -398,10 +398,10 @@ class ContractController extends mfBaseController {
|
||||
$contract_data['matchcode'] = trim($r->matchcode);
|
||||
$contract_data['termination_id'] = $r->termination_id;
|
||||
$contract_data['amount'] = 1;
|
||||
$contract_data['price'] = $r->price;
|
||||
$contract_data['price_setup'] = $r->price_setup;
|
||||
$contract_data['price_nne'] = $r->price_nne;
|
||||
$contract_data['price_nbe'] = $r->price_nbe;
|
||||
$contract_data['price'] = Layout::commaToDot(trim($r->price));
|
||||
$contract_data['price_setup'] = Layout::commaToDot(trim($r->price_setup));
|
||||
$contract_data['price_nne'] = Layout::commaToDot(trim($r->price_nne));
|
||||
$contract_data['price_nbe'] = Layout::commaToDot(trim($r->price_nbe));
|
||||
$contract_data['note'] = trim($r->note);
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,319 +1,340 @@
|
||||
<?php
|
||||
|
||||
class FronkDB {
|
||||
|
||||
public $link;
|
||||
private $result;
|
||||
private $lastError;
|
||||
private $log;
|
||||
public $link;
|
||||
private $result;
|
||||
private $lastError;
|
||||
private $log;
|
||||
|
||||
private static $instances = [];
|
||||
//private static $instance;
|
||||
private static $instances = [];
|
||||
|
||||
public function __construct($host=false,$user=false,$pass=false,$db=false) {
|
||||
$this->host=$host;
|
||||
$this->user=$user;
|
||||
$this->pass=$pass;
|
||||
$this->db=$db;
|
||||
|
||||
if(!$host) $this->host=FRONKDB_DBHOST;
|
||||
if(!$user) $this->user=FRONKDB_DBUSER;
|
||||
if(!$pass) $this->pass=FRONKDB_DBPASS;
|
||||
if(!$db) $this->db=FRONKDB_DBNAME;
|
||||
|
||||
$this->connect();
|
||||
$this->log = mfLoghandler::singleton();
|
||||
}
|
||||
|
||||
public static function singleton($host=false,$user=false,$pass=false,$db=false) {
|
||||
$instance_name = $host.$user.$db;
|
||||
if(!$instance_name) {
|
||||
$instance_name = "default";
|
||||
//private static $instance;
|
||||
|
||||
public function __construct($host = false, $user = false, $pass = false, $db = false) {
|
||||
$this->host = $host;
|
||||
$this->user = $user;
|
||||
$this->pass = $pass;
|
||||
$this->db = $db;
|
||||
|
||||
if(!$host) $this->host = FRONKDB_DBHOST;
|
||||
if(!$user) $this->user = FRONKDB_DBUSER;
|
||||
if(!$pass) $this->pass = FRONKDB_DBPASS;
|
||||
if(!$db) $this->db = FRONKDB_DBNAME;
|
||||
|
||||
$this->connect();
|
||||
$this->log = mfLoghandler::singleton();
|
||||
}
|
||||
|
||||
public static function singleton($host = false, $user = false, $pass = false, $db = false) {
|
||||
$instance_name = $host . $user . $db;
|
||||
if(!$instance_name) {
|
||||
$instance_name = "default";
|
||||
}
|
||||
|
||||
if(!isset(self::$instances[$instance_name])) {
|
||||
//echo "new FronkDB instance $instance_name\n";
|
||||
$c = __CLASS__;
|
||||
self::$instances[$instance_name] = new $c($host, $user, $pass, $db);
|
||||
}
|
||||
return self::$instances[$instance_name];
|
||||
}
|
||||
|
||||
private function connect() {
|
||||
//echo "FronkDB connection to ".$this->host." (".$this->user.", ".$this->db.")\n";
|
||||
if(!$this->link) {
|
||||
$this->link = mysqli_connect($this->host, $this->user, $this->pass, $this->db) or die("Error connecting to database..." . mysqli_error($this->link));
|
||||
}
|
||||
mysqli_select_db($this->link, $this->db) or die("Error connecting to database..." . mysqli_error($this->link));
|
||||
if(function_exists("mysqli_set_charset")) {
|
||||
mysqli_set_charset($this->link, 'utf8mb4');
|
||||
} else {
|
||||
$this->query("SET NAMES utf8mb4");
|
||||
}
|
||||
}
|
||||
|
||||
if(!isset(self::$instances[$instance_name])) {
|
||||
//echo "new FronkDB instance $instance_name\n";
|
||||
$c = __CLASS__;
|
||||
self::$instances[$instance_name] = new $c($host,$user,$pass,$db);
|
||||
}
|
||||
return self::$instances[$instance_name];
|
||||
}
|
||||
|
||||
private function connect() {
|
||||
//echo "FronkDB connection to ".$this->host." (".$this->user.", ".$this->db.")\n";
|
||||
if(!$this->link) {
|
||||
$this->link=mysqli_connect($this->host,$this->user,$this->pass,$this->db) or die("Error connecting to database...".mysqli_error($this->link));
|
||||
}
|
||||
mysqli_select_db($this->link,$this->db) or die("Error connecting to database...".mysqli_error($this->link));
|
||||
if(function_exists("mysqli_set_charset")) {
|
||||
mysqli_set_charset($this->link,'utf8mb4');
|
||||
} else {
|
||||
$this->query("SET NAMES utf8mb4");
|
||||
}
|
||||
}
|
||||
|
||||
public function reconnect() {
|
||||
$this->instances = [];
|
||||
$this->link = false;
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
public function disconnect() {
|
||||
if($this->link) {
|
||||
mysqli_close($this->link);
|
||||
$this->link=false;
|
||||
}
|
||||
}
|
||||
|
||||
public function escape($string) {
|
||||
return mysqli_real_escape_string($this->link,$string);
|
||||
}
|
||||
|
||||
public function query($sql,$print_error=false) {
|
||||
if(!$this->link) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
if(FRONKDB_SQLDEBUG==true) {
|
||||
/*$this->log->debug("[FronkDB] ================");
|
||||
$this->log->debug("[FronkDB] START SQL QUERY");
|
||||
$bt = debug_backtrace();
|
||||
foreach($bt as $n => $b) {
|
||||
$this->log->debug($n.") ".$b["file"]."(".$b['line']."): ".$b['class']."->".$b['function']."()" );
|
||||
}*/
|
||||
$this->log->debug("[FronkDB] $sql");
|
||||
//$this->log->debug("[FronkDB] END SQL QUERY");
|
||||
//echo "$sql\n";
|
||||
}
|
||||
|
||||
try {
|
||||
if($this->result=mysqli_query($this->link,$sql)) {
|
||||
return $this->result;
|
||||
} else {
|
||||
$this->lastError=mysqli_error($this->link);
|
||||
if($print_error) {
|
||||
echo "Error in SQL-query:<br />\n".$sql."<br />\n".$this->lastError."<br />\n";
|
||||
public function disconnect() {
|
||||
if($this->link) {
|
||||
mysqli_close($this->link);
|
||||
$this->link = false;
|
||||
}
|
||||
$this->log->warn("SQL Query failed: $sql");
|
||||
return false;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->log->warn("SQL Query Exception ".$e->getCode().": ". $e->getMessage()."\nQuery was:\n$sql");
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function getLastError() {
|
||||
return $this->lastError;
|
||||
}
|
||||
public function escape($string) {
|
||||
return mysqli_real_escape_string($this->link, $string);
|
||||
}
|
||||
|
||||
public function num_rows($_res=false) {
|
||||
$rows=false;
|
||||
$res=$this->result;
|
||||
public function query($sql, $print_error = false) {
|
||||
if(!$this->link) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
if($_res)
|
||||
$res=$_res;
|
||||
if(FRONKDB_SQLDEBUG == true) {
|
||||
/*$this->log->debug("[FronkDB] ================");
|
||||
$this->log->debug("[FronkDB] START SQL QUERY");
|
||||
$bt = debug_backtrace();
|
||||
foreach($bt as $n => $b) {
|
||||
$this->log->debug($n.") ".$b["file"]."(".$b['line']."): ".$b['class']."->".$b['function']."()" );
|
||||
}*/
|
||||
$this->log->debug("[FronkDB] $sql");
|
||||
//$this->log->debug("[FronkDB] END SQL QUERY");
|
||||
//echo "$sql\n";
|
||||
}
|
||||
|
||||
if(!$res)
|
||||
return 0;
|
||||
try {
|
||||
$this->result = mysqli_query($this->link, $sql);
|
||||
$return = $this->result;
|
||||
$log_query = false;
|
||||
|
||||
if($rows=mysqli_num_rows($res))
|
||||
return $rows;
|
||||
// check for errors
|
||||
if(!$this->result) {
|
||||
$return = false;
|
||||
$this->lastError = mysqli_error($this->link);
|
||||
if($print_error) {
|
||||
echo "Error in SQL-query:<br />\n" . $sql . "<br />\n" . $this->lastError . "<br />\n";
|
||||
}
|
||||
$this->log->warn("SQL Last Error: ".$this->lastError);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// check for warnings
|
||||
if(mysqli_warning_count($this->link) > 0) {
|
||||
$log_query = true;
|
||||
$warning = mysqli_get_warnings($this->link);
|
||||
if($warning !== false) {
|
||||
do {
|
||||
$this->log->warn("SQL Warning: ".$warning->errno." - ".$warning->message);
|
||||
} while ($warning->next());
|
||||
}
|
||||
}
|
||||
|
||||
public function fetch_array($_res=false) {
|
||||
$array=false;
|
||||
$res=$this->result;
|
||||
if($log_query) {
|
||||
$this->log->warn("SQL Query was: $sql");
|
||||
}
|
||||
|
||||
if($_res)
|
||||
$res=$_res;
|
||||
return $return;
|
||||
|
||||
if(!$res)
|
||||
return false;
|
||||
} catch(Exception $e) {
|
||||
$this->log->warn("SQL Query Exception " . $e->getCode() . ": " . $e->getMessage() . "\nQuery was:\n$sql\n".$e->getTraceAsString());
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
if($array=mysqli_fetch_assoc($res))
|
||||
return $array;
|
||||
public function getLastError() {
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public function num_rows($_res = false) {
|
||||
$rows = false;
|
||||
$res = $this->result;
|
||||
|
||||
public function fetch_object($_res=false) {
|
||||
$obj=false;
|
||||
$res=$this->result;
|
||||
if($_res)
|
||||
$res = $_res;
|
||||
|
||||
if($_res)
|
||||
$res=$_res;
|
||||
if(!$res)
|
||||
return 0;
|
||||
|
||||
if(!$res)
|
||||
return false;
|
||||
if($rows = mysqli_num_rows($res))
|
||||
return $rows;
|
||||
|
||||
if($obj=mysqli_fetch_object($res))
|
||||
return $obj;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public function fetch_array($_res = false) {
|
||||
$array = false;
|
||||
$res = $this->result;
|
||||
|
||||
public function insert($_table,$_data,$_forcestr=array(),$options=array()) {
|
||||
if(empty($_table)) {
|
||||
$this->lastError="Error constructing INSERT: tablename ommited";
|
||||
return false;
|
||||
}
|
||||
if($_res)
|
||||
$res = $_res;
|
||||
|
||||
$table=$_table;
|
||||
if(!$res)
|
||||
return false;
|
||||
|
||||
$fields="";
|
||||
$values="";
|
||||
$STRINGS=$_forcestr;
|
||||
if($array = mysqli_fetch_assoc($res))
|
||||
return $array;
|
||||
|
||||
// Build INSERT
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($_data as $f=>$v) {
|
||||
$_Q="'";
|
||||
if(is_string($v)) {
|
||||
$v=$this->escape($v);
|
||||
}
|
||||
if(is_numeric($v)) {
|
||||
if(!in_array($f,$STRINGS)) {
|
||||
$_Q="";
|
||||
}
|
||||
}
|
||||
if($v === null) {
|
||||
$_Q = '';
|
||||
$v = "NULL";
|
||||
}
|
||||
public function fetch_object($_res = false) {
|
||||
$obj = false;
|
||||
$res = $this->result;
|
||||
|
||||
$fields.=",`$f`";
|
||||
$values.=",".$_Q.$v.$_Q;
|
||||
}
|
||||
if($_res)
|
||||
$res = $_res;
|
||||
|
||||
$fields=preg_replace('/^,/','',$fields);
|
||||
$values=preg_replace('/^,/','',$values);
|
||||
if(!$res)
|
||||
return false;
|
||||
|
||||
$SQLstr="INSERT INTO `$table` ($fields) VALUES($values)";
|
||||
if($obj = mysqli_fetch_object($res))
|
||||
return $obj;
|
||||
|
||||
if(!$this->query($SQLstr)) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
public function insert($_table, $_data, $_forcestr = array(), $options = array()) {
|
||||
if(empty($_table)) {
|
||||
$this->lastError = "Error constructing INSERT: tablename ommited";
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update($_table,$_data,$_where,$_forcestr=array()) {
|
||||
if(empty($_table)) {
|
||||
$this->lastError="Error constructing UPDATE: tablename ommited";
|
||||
return false;
|
||||
}
|
||||
$table = $_table;
|
||||
|
||||
if(empty($_where)) {
|
||||
$this->lastError="Security breach on all decks! UPDATE without WHERE clause.";
|
||||
return false;
|
||||
}
|
||||
$fields = "";
|
||||
$values = "";
|
||||
$STRINGS = $_forcestr;
|
||||
|
||||
$table=$_table;
|
||||
// Build INSERT
|
||||
|
||||
$Pairs="";
|
||||
$where="WHERE $_where";
|
||||
$STRINGS=$_forcestr;
|
||||
foreach($_data as $f => $v) {
|
||||
$_Q = "'";
|
||||
if(is_string($v)) {
|
||||
$v = $this->escape($v);
|
||||
}
|
||||
if(is_numeric($v)) {
|
||||
if(!in_array($f, $STRINGS)) {
|
||||
$_Q = "";
|
||||
}
|
||||
}
|
||||
if($v === null) {
|
||||
$_Q = '';
|
||||
$v = "NULL";
|
||||
}
|
||||
|
||||
$fields .= ",`$f`";
|
||||
$values .= "," . $_Q . $v . $_Q;
|
||||
}
|
||||
|
||||
$fields = preg_replace('/^,/', '', $fields);
|
||||
$values = preg_replace('/^,/', '', $values);
|
||||
|
||||
$SQLstr = "INSERT INTO `$table` ($fields) VALUES($values)";
|
||||
|
||||
if(!$this->query($SQLstr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function update($_table, $_data, $_where, $_forcestr = array()) {
|
||||
if(empty($_table)) {
|
||||
$this->lastError = "Error constructing UPDATE: tablename ommited";
|
||||
return false;
|
||||
}
|
||||
|
||||
if(empty($_where)) {
|
||||
$this->lastError = "Security breach on all decks! UPDATE without WHERE clause.";
|
||||
return false;
|
||||
}
|
||||
|
||||
$table = $_table;
|
||||
|
||||
$Pairs = "";
|
||||
$where = "WHERE $_where";
|
||||
$STRINGS = $_forcestr;
|
||||
|
||||
|
||||
// Build field/value pairs string
|
||||
// Build field/value pairs string
|
||||
|
||||
foreach($_data as $f => $v)
|
||||
{
|
||||
$_Q="'";
|
||||
if(is_string($v)) {
|
||||
$v=$this->escape($v);
|
||||
}
|
||||
if(is_numeric($v)) {
|
||||
if(!in_array($f,$STRINGS)) {
|
||||
$_Q='';
|
||||
}
|
||||
}
|
||||
if($v === null) {
|
||||
$_Q = '';
|
||||
$v = "NULL";
|
||||
}
|
||||
foreach($_data as $f => $v) {
|
||||
$_Q = "'";
|
||||
if(is_string($v)) {
|
||||
$v = $this->escape($v);
|
||||
}
|
||||
if(is_numeric($v)) {
|
||||
if(!in_array($f, $STRINGS)) {
|
||||
$_Q = '';
|
||||
}
|
||||
}
|
||||
if($v === null) {
|
||||
$_Q = '';
|
||||
$v = "NULL";
|
||||
}
|
||||
|
||||
$Pairs.=", `$f`=".$_Q.$v.$_Q;
|
||||
}
|
||||
$Pairs .= ", `$f`=" . $_Q . $v . $_Q;
|
||||
}
|
||||
|
||||
$Pairs=preg_replace('/^,/','',$Pairs);
|
||||
$Pairs = preg_replace('/^,/', '', $Pairs);
|
||||
|
||||
$SQLstr="UPDATE `$table` SET $Pairs $where";
|
||||
|
||||
if(!$this->query($SQLstr)) {
|
||||
return false;
|
||||
}
|
||||
$SQLstr = "UPDATE `$table` SET $Pairs $where";
|
||||
|
||||
return true;
|
||||
if(!$this->query($SQLstr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
|
||||
public function select($_table,$_fields="*",$_where="") {
|
||||
$table=$_table;
|
||||
$fields=$_fields;
|
||||
$where="";
|
||||
}
|
||||
|
||||
if(!empty($_where)) {
|
||||
$where="WHERE $_where";
|
||||
}
|
||||
public function select($_table, $_fields = "*", $_where = "") {
|
||||
$table = $_table;
|
||||
$fields = $_fields;
|
||||
$where = "";
|
||||
|
||||
if(empty($_table)) {
|
||||
$this->lastError="No table specified";
|
||||
return false;
|
||||
}
|
||||
if(!empty($_where)) {
|
||||
$where = "WHERE $_where";
|
||||
}
|
||||
|
||||
if(empty($_fields))
|
||||
$fields="*";
|
||||
if(empty($_table)) {
|
||||
$this->lastError = "No table specified";
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!is_array($fields)) {
|
||||
$fields=preg_split('/ ?, ?/',$fields);
|
||||
}
|
||||
|
||||
$fstr = "";
|
||||
if(empty($_fields))
|
||||
$fields = "*";
|
||||
|
||||
foreach($fields as $f)
|
||||
{
|
||||
$fstr.=",`$f`";
|
||||
if($f=="*") $fstr="*";
|
||||
}
|
||||
if(!is_array($fields)) {
|
||||
$fields = preg_split('/ ?, ?/', $fields);
|
||||
}
|
||||
|
||||
$fstr=preg_replace('/^,/','',$fstr);
|
||||
$SQLstr="SELECT $fstr FROM `$table` $where";
|
||||
|
||||
if($res=$this->query($SQLstr)) {
|
||||
return $res;
|
||||
}
|
||||
$fstr = "";
|
||||
|
||||
return false;
|
||||
foreach($fields as $f) {
|
||||
$fstr .= ",`$f`";
|
||||
if($f == "*") $fstr = "*";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function delete($_table,$_where,$_limit=false) {
|
||||
if(empty($_where)) {
|
||||
$this->lastError="Security breach on all decks! DELETE without WHERE clause!";
|
||||
return false;
|
||||
}
|
||||
|
||||
if(empty($_table)) {
|
||||
$this->lastError="Keine Tabelle angegeben!";
|
||||
return false;
|
||||
}
|
||||
|
||||
$table=$_table;
|
||||
$where=$_where;
|
||||
$limit="";
|
||||
if($_limit) {
|
||||
$limit="LIMIT $_limit";
|
||||
}
|
||||
|
||||
$sql="DELETE FROM `$table` WHERE $where $limit";
|
||||
//echo $sql;
|
||||
if($this->query($sql)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$fstr = preg_replace('/^,/', '', $fstr);
|
||||
$SQLstr = "SELECT $fstr FROM `$table` $where";
|
||||
|
||||
if($res = $this->query($SQLstr)) {
|
||||
return $res;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public function delete($_table, $_where, $_limit = false) {
|
||||
if(empty($_where)) {
|
||||
$this->lastError = "Security breach on all decks! DELETE without WHERE clause!";
|
||||
return false;
|
||||
}
|
||||
|
||||
if(empty($_table)) {
|
||||
$this->lastError = "Keine Tabelle angegeben!";
|
||||
return false;
|
||||
}
|
||||
|
||||
$table = $_table;
|
||||
$where = $_where;
|
||||
$limit = "";
|
||||
if($_limit) {
|
||||
$limit = "LIMIT $_limit";
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM `$table` WHERE $where $limit";
|
||||
//echo $sql;
|
||||
if($this->query($sql)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user