php 2007-2009 Barttos Vartolomei Nicolae All rights reserved http bart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* (c) 2007-2009 Barttos (Vartolomei Nicolae) - All rights reserved.
* http://barttos.net/
* me@barttos.net
* nvartolomei@gmail.com
*/
class bts_mysqli {
private $connections = array(); # multiple database connection
private $active_connection = 0; # we need to know wich connection need to use
private $query_cache = array(); # queries wich have been executed, but "saved for later"
private $data_cache = array(); # data "saved for later"
private $last; # last query
public function __construct() {
}
# create a new database connection, return the int id of connection
public function new_connection($host, $user, $password, $database) {
$this->connections[] = new mysqli($host, $user, $password, $database);
$connection_id = count($this->connections)-1;
if (mysqli_connect_errno()) {
trigger_error("Error connecting to host. " . $this->conections[$connection_id]->error, E_USER_ERROR);
}
return $connection_id;
}
public function close_connection() {
$this->connections[$this->active_connection]->close();
}
public function set_active_connection(int $id) {
$this->active_connection = $id;
}
public function exec_query($query_str) {
if (!$result = $this->connections[$this->active_connection]->query($query_str)) {
trigger_error("Error executing query: ".$this->connections[$this->active_connection]->error, E_USER_ERROR);
} else {
$this->last = $result;
}
}
public function insert_records($table, $data) {
$fields = null;
$values = null;
foreach ($data as $field => $value) {
$fields .= "`$field`,";
$values .= (is_numeric($value) && (intval($value) == $value)) ? $value . "," : "'$value',";
}
$fields = substr($fields, 0, -1);
$values = substr($values, 0, -1);
$this->exec_query("INSERT INTO $table (" . $fields . ") VALUES(" . $values . ")");
return True;
}
public function update_records($table, $changes, $condition) {
$update = "UPDATE " . $table . " SET ";
foreach($changes as $field => $value) {
$update .= "`" . $field . "`='{$value}',";
}
$update = substr($update, 0, -1);
if ($condition != "") {
$update .= "WHERE " . $condition;
}
$this->exec_query($update);
return true;
}
public function delete_records($table, $condition, $limit) {
$limit = ($limit == '') ? '' : ' LIMIT ' . $limit;
$delete = "DELETE FROM " . $table . " WHERE " . $condition . $limit;
$this->exec_query($delete);
}
public function get_rows() {
return $this->last->fetch_object();
}
public function affected_rows() {
return $this->connections[$this->activeConnection]->affected_rows;
}
public function sanitize_data($data) {
return $this->connections[$this->active_connection]->real_escape_string($data);
}
}