-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
77 lines (65 loc) · 2.33 KB
/
Database.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
<?php
namespace WpStaging\AbTesting;
use PDO;
use PDOException;
if (!defined('ABSPATH')) {
exit;
}
class Database
{
private PDO $pdo;
private string $tableName;
public function __construct()
{
// Define your database credentials and table name
$dbHost = '172.201.0.1'; // Change as needed
$dbName = 'wpstaging'; // Change as needed
$dbUser = 'admin'; // Change as needed
$dbPassword = 'admin'; // Change as needed
$this->tableName = 'wpstgab_test_results'; // Table to store A/B test results
try {
$dsn = "mysql:host=$dbHost;dbname=$dbName;charset=utf8mb4";
$this->pdo = new PDO($dsn, $dbUser, $dbPassword);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die('Database connection failed: ' . $e->getMessage());
}
// Ensure the table exists
$this->createTableIfNotExists();
}
// Create the table if it doesn't already exist
private function createTableIfNotExists()
{
$sql = "
CREATE TABLE IF NOT EXISTS $this->tableName (
id INT AUTO_INCREMENT PRIMARY KEY,
test_name VARCHAR(255),
variant VARCHAR(50),
event VARCHAR(255),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
";
try {
$this->pdo->exec($sql);
} catch (PDOException $e) {
die('Failed to create table: ' . $e->getMessage());
}
}
// Insert A/B test event into the database
public function insertTestEvent($testName, $variant, $event): bool
{
$sql = "INSERT INTO $this->tableName (test_name, variant, event) VALUES (:test_name, :variant, :event)";
try {
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':test_name', $testName);
$stmt->bindParam(':variant', $variant);
$stmt->bindParam(':event', $event);
$stmt->execute();
} catch (PDOException $e) {
error_log('Failed to insert A/B test event: ' . $e->getMessage());
return false;
}
return true;
}
}