-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdo-library.php
143 lines (129 loc) · 4.21 KB
/
pdo-library.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
<?php
function pdo_get_connection()
{
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=duan1_database;charset=utf8", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
return $conn;
}
function pdo_execute($sql)
{
$sql_args = array_slice(func_get_args(), 1);
try {
$conn = pdo_get_connection();
$stmt = $conn->prepare($sql);
$stmt->execute($sql_args);
} catch (PDOException $e) {
throw $e;
} finally {
unset($conn);
}
return "Successfuly!";
}
//truy vấn nhiều dữ liệu
function pdo_query($sql)
{
$sql_args = array_slice(func_get_args(), 1);
try {
$conn = pdo_get_connection();
$stmt = $conn->prepare($sql);
$stmt->execute($sql_args);
$rows = $stmt->fetchAll();
return $rows;
} catch (PDOException $e) {
throw $e;
} finally {
unset($conn);
}
}
//truy vấn 1 dữ liệu
function pdo_query_one($sql)
{
$sql_args = array_slice(func_get_args(), 1);
try {
$conn = pdo_get_connection();
$stmt = $conn->prepare($sql);
$stmt->execute($sql_args);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row;
} catch (PDOException $e) {
throw $e;
} finally {
unset($conn);
}
}
// Thư viện hàm này bị lỗi gì ? -->? Giải thích một xíu về thư viện này được không
function pdo_query_value($sql)
{
// var_dump(func_get_args());
$sql_args = array_slice(func_get_args(), 1);
$column_name = array_slice(func_get_args(), 0, 1);
// var_dump($column_name);
try {
$conn = pdo_get_connection();
$stmt = $conn->prepare($sql);
$stmt->execute($sql_args);
$row = $stmt->fetch(PDO::FETCH_NUM);
// var_dump($row);
if (!$row) {
return 0;
}
return $row[0];
} catch (PDOException $e) {
throw $e;
} finally {
unset($conn);
}
}
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
function sendmail($recipient_mail, $title, $message)
{
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // gửi mail SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'minhdantt4511'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress($recipient_mail, 'Customer'); // Add a recipient
// $mail->addAddress('[email protected]'); // Name is optional
// $mail->addReplyTo('[email protected]', 'Information');
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');
// Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $title;
$mail->Body = $message;
$mail->AltBody = '';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
exit;
}
}