-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincrement.php
34 lines (26 loc) · 972 Bytes
/
increment.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
<?php
$servername = "";
$user = "";
$password = "";
$dbname = "";
$host = "";
try {
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("CREATE TABLE IF NOT EXISTS counter_table (id INT PRIMARY KEY, count INT)");
$stmt->execute();
$stmt = $dbh->prepare("INSERT INTO counter_table (id, count) VALUES (1, 0) ON DUPLICATE KEY UPDATE count = count");
$stmt->execute();
$stmt = $dbh->prepare("SELECT count FROM counter_table WHERE id = 1");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $row['count'];
$count++;
$stmt = $dbh->prepare("UPDATE counter_table SET count = :count WHERE id = 1");
$stmt->bindParam(':count', $count);
$stmt->execute();
echo $count;
} catch (PDOException $e) {
echo "データベースエラー: " . $e->getMessage();
}
?>