forked from DaoCloud/php-apache-mysql-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB.php
72 lines (57 loc) · 1.92 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
<?php
require('./Helper.php');
class DB
{
protected $pdo;
function __construct()
{
$serverName = env("MYSQL_PORT_3306_TCP_ADDR", "localhost");
$databaseName = env("MYSQL_INSTANCE_NAME", "homestead");
$username = env("MYSQL_USERNAME", "homestead");
$password = env("MYSQL_PASSWORD", "secret");
try {
$this->pdo = new PDO("mysql:host=$serverName;dbname=$databaseName", $username, $password);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 检测数据库是否存在表
$isInstall = $this->pdo->query("SHOW TABLES like 'contacts';")
->rowCount();
if (!$isInstall) {
$sql = "
CREATE TABLE contacts (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL )
";
$this->pdo->exec($sql);
$sqlData = "
INSERT INTO `contacts` VALUES ('1', 'John', '188888888');
INSERT INTO `contacts` VALUES ('2', 'Bob', '166666666');
INSERT INTO `contacts` VALUES ('3', 'Zoe', '155555555');
";
$this->pdo->exec($sqlData);
}
} catch (PDOException $e) {
echo "数据库链接失败: " . $e->getMessage();
die();
}
}
public function all()
{
return $this->pdo->query('SELECT * from contacts')
->fetchAll();
}
public function find($id)
{
return $this->pdo->query("SELECT * from contacts WHERE id = $id ")
->fetch();
}
public function remove($id)
{
return $this->pdo->exec("DELETE from contacts WHERE id = $id ");
}
public function add($name, $phone)
{
$sql = "INSERT INTO contacts ( name , phone ) VALUES ('$name','$phone')";
return $this->pdo->exec($sql);
}
}