forked from OvidiuRusu/TW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasabd.php
More file actions
104 lines (97 loc) · 3.26 KB
/
clasabd.php
File metadata and controls
104 lines (97 loc) · 3.26 KB
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
<?php
abstract class DataBase_SQL { // clasa generica (abstracta)
// date-membru
private $Link_ID; // identif. legaturii cu o baza de date
private $Query_ID; // identif. interogarii active
private $Errno; // starea de eroare
// metode (generice)
abstract public function connect(); // conectare la serverul BD
abstract public function query($q); // trimiterea unei inter. SQL
abstract public function next_record(); // urmatoarea inreg. gasita
// eventual si altele...
}
class DataBase_MySQL extends DataBase_SQL { // clasa pentru MySQL
// date-membru
private $Host; // adresa serverului MySQL
private $DataBase; // numele bazei de date
private $User; // numele utilizatorului
private $Password; // parola utilizatorului
private $Row; // numarul rândului curent
private $Error; // mesajul de eroare
public $Record = array(); // contine inregistrarile gasite
// constructor
public function __construct ($h = '', $db = '', $u = '', $p = '') {
$this->Link_ID = 0;
$this->Query_ID = 0;
$this->Errno = 0;
$this->Host = $h;
$this->DataBase = $db;
$this->User = $u;
$this->Password = $p;
$this->Row = -1;
$this->Error = '';
}
// destructor
public function __destruct () {
if ($this->Link_ID) {
mysql_close ($this->Link_ID); // inchidem conexiunea
}
}
// metode:
// opreste executia în caz de eroare fatala
public function halt ($msg) {
printf("A survenit eroarea: %s\n", $msg);
printf("MySQL raporteaza: %s (%s)\n", $this->Errno, $this->Error);
die("Terminare anormala.");
}
// conectare la baza de date
public function connect () {
if ($this->Link_ID == 0) { // inca nu exista o conexiune
$this->Link_ID = mysql_connect (
$this->Host, $this->User, $this->Password);
if (!$this->Link_ID) { // succes sau esec?
$this->halt ("Conexiune esuata");
}
// deschidem baza de date
if (!mysql_select_db($this->DataBase, $this->Link_ID)) {
// salvam erorile
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$this->halt ("Baza de date " . $this->DataBase .
" nu poate fi deschisa");
}
}
}
// trimite o interogare serverului MySQL
public function query ($q) {
$this->connect(); // ne conectam...
// executam interogarea
$this->Query_ID = mysql_query ($q, $this->Link_ID);
// initial stabilim pointerul pe prima înregistrare
$this->Row = 0;
// salvam erorile
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID) { // eroare fatala?
$this->halt ("Comanda SQL eronata: " . $q);
}
return $this->Query_ID;
}
// furnizeaza daca mai exista o înregistrare
public function next_record() {
// salvam într-un tablou înregistrarile
$this->Record = mysql_fetch_array ($this->Query_ID);
$this->Row++;
// salvam erorile
$this->Errno = mysql_errno();
$this->Error = mysql_error();
// returnam inregistrarea gasita
$stat = is_array ($this->Record);
if (!$stat) { // nu mai exista o alta inregistrare
mysql_free_result ($this->Query_ID); // se elibereaza interogarea
$this->Query_ID = 0;
}
return $stat;
}
}
?>