-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.php
More file actions
72 lines (59 loc) · 1.48 KB
/
Copy pathSQL.php
File metadata and controls
72 lines (59 loc) · 1.48 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
<?php
namespace Pipe;
use PDO;
class SQL
{
public $connection = null;
public $stmt = null;
public $lastResult = false;
public function connect(
string $host,
string $user,
string $password,
string $dbname,
int $port = 3306,
string $encoding = "utf8"
): self
{
$connection = new PDO(
"mysql:dbname=$dbname;host=$host;charset=$encoding;port=$port",
$user,
$password,
);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$this->connection = $connection;
return $this;
}
public function raw(string $sql)
{
$this->lastResult = $this->stmt = $this->connection->query($sql);
return $this;
}
public function query(string $sql, array $args = [])
{
$this->stmt = $this->connection->prepare($sql);
$this->lastResult = $this->stmt->execute($args);
return $this;
}
public function all()
{
return $this->stmt->fetchAll();
}
public function first()
{
return $this->stmt->fetch();
}
public function one()
{
return $this->stmt->fetch(\PDO::FETCH_COLUMN);
}
public function lastInsertId(): int
{
return $this->connection->lastInsertId();
}
public function lastResult(): bool
{
return $this->lastResult;
}
}