-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.mysql.php
120 lines (106 loc) · 2.89 KB
/
class.mysql.php
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Datei: class.mysql.php
* Erstellungsdatum: 25.07.2010
* Letzte bearbeitung: -
* Beschreibung: MySQL Klasse
* Autor: Andreas Gyr
*/
class mysql {
private $connection = NULL;
public $result;
private $counter=NULL;
public $result_counter = 0;
public $x;
public $errors = true;
// MySQL Verbindung öffnen
public function __construct($host=NULL, $database=NULL, $user=NULL, $pass=NULL, $errors = 'on'){
if($errors == 'off') {
$this->errors = false;
}
$this->connection = mysql_connect($host,$user,$pass,TRUE);
mysql_select_db($database, $this->connection);
}
// MySQL Verbindung schliessen
public function disconnect() {
if (is_resource($this->connection))
mysql_close($this->connection);
}
// MySQL Anfrage
public function query($query) {
if($this->errors) {
$this->result = mysql_query($query,$this->connection)
or die('Error: '.mysql_error($this->connection).'<br /><br />'.$query);
} else {
$this->result = @mysql_query($query,$this->connection);
}
$this->counter=NULL;
return true;
}
public function insert($insert_array, $table) {
$sql = "INSERT INTO ".$table." SET ";
$c = count($insert_array); $i = 1;
foreach ($insert_array as $field => $value) {
$sql .= $field." = ".$value;
if ($c == $i) {
$sql .= ";";
} else {
$sql .= ", ";
}
$i++;
}
$this->query($sql);
return $this->result;
}
public function update($update_array, $table, $id) {
$sql = "UPDATE ".$table." SET ";
$c = count($update_array); $i = 1;
foreach ($update_array as $field => $value) {
$sql .= $field." = ".$value;
if ($c != $i) {
$sql .= ", ";
}
$i++;
}
$sql .= " WHERE id = ".$id.";";
$this->query($sql);
return $this->result;
}
public function id_select($fields, $table, $id) {
$sql = "SELECT ".$fields." FROM ".$table." WHERE id = ".$id.";";
$this->query($sql);
return $this->result;
}
public function select($fields, $table, $zusatz = '') {
$this->result_counter++;
$sql = "SELECT ".$fields." FROM ".$table." ".$zusatz.";";
$this->query($sql);
return $this->result;
}
public function delete($table, $id) {
$sql = "DELETE FROM ".$table." WHERE id = ".$id.";";
$this->query($sql);
return $this->result;
}
public function get_from_id($field, $table, $id) {
$sql = "SELECT ".$field." FROM ".$table." WHERE id = ".$id.";";
$this->query($sql);
$row = $this->fetchRow();
return $row[$field];
}
public function fetchRow($result = false) {
return mysql_fetch_assoc($result ? $result : $this->result);
}
public function insert_id() {
return mysql_insert_id($this->connection);
}
public function count($result = false) {
if($result && is_resource($result)) {
$this->counter=mysql_num_rows($result);
} elseif($this->counter==NULL && is_resource($this->result)) {
$this->counter=mysql_num_rows($this->result);
}
return $this->counter;
}
}
?>