-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqli_extended.php
82 lines (75 loc) · 2.69 KB
/
mysqli_extended.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
<?php
/**
* Onur Canalp
* Mysqli DB bağlantı ve hata sınıfımız.. Basit anlamda örnek olması için..
*/
/*
* MySQL Server a bağlantıda hata yaşarsak bu Exception sınıfından türettiğimiz hatayı döndüreceğiz.
*/
class DBConnectException extends Exception
{
public function __construct($error, $errno = 0)
{
parent::__construct($error, $errno);
}
}
/**
* DB Query Exception class
*
* SQL sorgumuzu çalıştırırken oluşan hatalar için bu sınıfı kullanacağız..
*/
class DBQueryException extends Exception
{
public function __construct($error, $errno = 0)
{
parent::__construct($error, $errno);
}
}
/**
* mysqli sınıfından türettiğimiz DB sınıfımız
*
*/
class Database extends mysqli
{
/**
* __construct overwrite edelim. Bağlantı hataları durumunda Hata durumunda DBConnectException ları tutacağız
*
* @param string $host MySQL Host
* @param string $user MySQL Kullanıcı Adı
* @param string $pass MySQL Parola (password kullanmamak için null kullanın)
* @param string $db MySQL Bağlanacağımız Veritabanı adı (kullanmamak için null kullanın)
* @param string $port MySQL Bağlanacağımız port (kullanmamak için null kullanın)
* @param string $socket MySQL Kullanılacak soket (kullanmamak için null kullanın)
* @throws DBConnectException
*/
public function __construct($host = 'localhost', $user = null, $pass = null, $db = null, $port = null, $socket = null)
{
@parent::__construct($host, $user, $pass, $db, $port, $socket);
if ($this->connect_errno != 0) {
// Hata olursa DBConnectException döndüreceğiz error message ve error code ile
throw new DBConnectException($this->connect_error, $this->connect_errno);
}
}
/**
* Query metodumuz
*
* @param string $sql Çalıştırılacak SQL sorgusu
* @return mysqli_result Object
* @throws DBQueryException
*/
public function query($sql)
{
// sql.log dosyamızda log tutacağız
file_put_contents('/tmp/sql.log', $sql . "\n", FILE_APPEND);
// query çalıştırılırken, parent query metodumuzu çağırıyoruz yani mysqli sınıfında ki
// @ operatorü ile çağırdık gib gelebilecek hataları temizleyelim
$result = @parent::query($sql);
// errno set edilmiş mi varmı bakalım
if ($this->errno != 0) {
// kendi DBQueryException ımızı hata mesajı ve hata numarası ile döndürelim
throw new DBQueryException($this->error, $this->errno);
}
// Herşey düzgünse mysqli_result object ini döndürelim
return $result;
}
}