forked from muatik/mysqlDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
executable file
·153 lines (135 loc) · 3.93 KB
/
db.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
PHP 4.4.2 sürümünde yazýldý ve denendi. Bu sýnýf mysql veri tabaný sunucusu üzerinde iþlem yapmayý kolaþtýrmak amacýyla yazýldý.
Gerekli veri tabaný hata ve kontrol kodlarýný her uygulamada tekrardan yazmaktansa bu sýnýfý kullanarak zaman kazanýlabilir.
NOT:PHP5 ve daha üst bir sürüm kullanýyorsanýz bu sýnýf çalýþmayabilir. Çünki php5 sürümünde sýnýflar konusunda köklü deðiþiklikler yapýldý.
mustafa
*/
class db{
var $host='localhost';
var $username='root';
var $password='root';
var $database='test';
var $connection;
var $reader;
var $affectedRows;
var $numRows;
var $error;
var $charSet='utf8';
var $collate='utf8_turkish_ci';
public function __construct(){
/*
* eğer veritabanı bağlantı sabitleri tanımlandıysa
* */
if(defined('_dbHost')!=null){
$this->host=constant('_dbHost');
$this->username=constant('_dbUser');
$this->password=constant('_dbPassword');
if(constant('_dbDatabase')!=null)
$this->database=constant('_dbDatabase');
}
}
function connect(){
if($this->connection=new mysqli($this->host,$this->username,$this->password)){
$this->query('set names "'.$this->charSet.'" collate "'.$this->collate.'"');
if(@$this->connection->select_db($this->database)){
$this->query('set names "'.$this->charSet.'" collate "'.$this->collate.'"');
return true;
}
$this->error='Veri tabaný seçilemedi.';
return false;
}
$this->error='Veri tabaný sunucusuna baðlanýlamadý.';return false;
}
function query($sql,$buffered=true){
$this->affectedRows=0;
$this->numRows=0;
if(!$this->connection && !$this->connect()) return false;
if(($buffered && $this->reader=$this->connection->query($sql)) ||
(!$buffered && $this->reader=$this->connection->query($sql))){
if(gettype($this->reader)=='object')
$this->numRows=$this->reader->num_rows;
else
$this->affectedRows=$this->connection->affected_rows;
return true;
}
$this->error='Sorgu çalýþtýrýlamadý.';return false;
}
function unbufferedQuery($sql){
return $this->query($sql,false);
}
function fetchObject(){
return $this->reader->fetch_object();
}
function fetchArray(){
return $this->reader->fetch_array();
}
function fetchRow(){
return $this->reader->fetch_row();
}
function nextIncrement($t){
$this->query('show table status like \''.$t.'\'');
$nau=$this->fetchObject(); // next auto_increment
return $nau->Auto_increment;
}
function lastIncrement($t){
return $this->nextIncrement($t)-1;
}
function getInsertId(){
return $this->connection->insert_id;
}
function getError(){
return $this->connection->error;
}
function fetchListByQuery($sql,$style='object'){
if($this->query($sql) ){
$arr=array();
if($style=='object')
while($r=$this->fetchObject()) $arr[]=$r;
elseif($style=='array')
while($r=$this->fetchArray()) $arr[]=$r;
elseif($style=='row')
while($r=$this->fetchRow()) $arr[]=$r;
return $arr;
}
return false;
}
function fetchFirstRecord($q){
if($this->query($q)){
if($this->numRows>0)
return $this->fetchObject();
}
return false;
}
/*
* aşağıdakiler yeni metodlardır.
* üstekilerle değiştirilecektir zamanla
* */
public function fetch($sql,$style='object'){
return $this->fetchListByQuery($sql,$style='object');
}
public function fetchFirst($q){
return $this->fetchFirstRecord($q);
}
public function escape($s,$strip=true){
if(!$this->connection && !$this->connect()) return false;
if(!is_array($s)){
if($strip){
if(strpos($s,'\\\'')!==false || strpos($s,'\\"')!==false)
$s=stripslashes($s);
}
return $this->connection->real_escape_string($s);
}
else{
if($strip){
foreach($s as $k=>$i)
if(strpos($i,'\\\'')!==false || strpos($i,'\\"')!==false)
$s[$k]=stripslashes($i);
}
foreach($s as $k=>$i)
$s[$k]=$this->connection->real_escape_string($i);
return $s;
}
}
}
?>