-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyPDO.php
More file actions
executable file
·136 lines (104 loc) · 3.99 KB
/
Copy pathMyPDO.php
File metadata and controls
executable file
·136 lines (104 loc) · 3.99 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
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
<?PHP
namespace MzQ;
use \PDO;
use MzQ\Core\Errors;
use PDOException;
class MyPDO extends PDO {
public function __construct() {
//ساخت شی جدید از دیتابیس
try {
//اطلاعات اتصال به دیتابیس
parent::__construct("mysql:host=" . Config::servername . ";dbname=" .
Config::dbname, Config::username, Config::password );
self::$instance = $this;//ذخیره شی در شی ثابت
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//نمایش ارور ها
self::$instance->exec("set names utf8");//اتصال برای نمایش کاراکتر فارسی
} catch (PDOException $e) {
new Errors($e) ;
}
}
private function __clone()
{
}
public static function getInstance(){
if (self::$instance==null) { // اگر شی قبلی از دیتابیس وجود نداشت
try {
// یک شی جدید از دیتابیس می سازیم
self::$instance = new MyPDO("mysql:host=" . Config::servername . ";dbname=" . Config::dbname, Config::username, Config::password);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//نمایش ارور ها
self::$instance->exec("set names utf8");//اتصال برای نمایش کاراکتر فارسی
} catch (PDOException $e) {
new Errors($e) ;
}
}
return self::$instance;//بازگرداندن شی به خروجی
}
// این شی درطول دوره حیات برنامه رابط ما و دیتابیس خواهد بود
static private $instance = null;
public static function getRowCount($stmt)
{
//نمایش تعداد سطرهایی که در جواب دیتابیس برگرداند
return $stmt->rowCount() ;
}
public static function getError($stmt)
{
//دریافت ارور دیتابیس
return $stmt->errorInfo() ;
}
public static function getLastInsertId($conn) {
return $conn->lastInsertId();
}
/**
*
* @param String $sql SQL Query
* @param Array $values array to bind with sql query
* @param Boolean $autoErroResponder automatically send json response on error
* @param Boolean $fetchAll fetch all items
* @param Integer $fetchStyle
* @return Array or \PDOException
*/
public static function doSelect($sql, $values = array(), $autoErroResponder = false , $fetchAll = true, $fetchStyle = PDO::FETCH_ASSOC)
{
$conn = MyPDO::getInstance();
$stmt = $conn->prepare($sql);
$result = null;
if($values != NULL) {
foreach ($values as $key => $value) {
$stmt->bindValue($key + 1, $value);
}
}
try {
$stmt->execute();
if ($fetchAll) {
$result = $stmt->fetchAll($fetchStyle);
} else {
$result = $stmt->fetch($fetchStyle);
}
return $result;
} catch (\PDOException $ex) {
if($autoErroResponder) new Errors("Internal Server Error" ) ;
return $ex;
}
}
/**
*
* @param String $sql SQL Query
* @param Array $values array to bind with sql query
* @param Boolean $autoErroResponder automatically send json response on error
* @return Ineteger or \PDOException
*/
public static function doQuery($sql, $values = [] , $autoErroResponder = false )
{
$stmt = self::getInstance()->prepare($sql);
foreach ($values as $key => $value) {
$stmt->bindValue($key + 1, $value);
}
try {
$stmt->execute();
} catch (\PDOException $ex) {
if($autoErroResponder) new Errors("Internal Server Error") ;
return $ex;
}
return $stmt->rowCount();
}
}