Repository navigation
Expand file tree
/
Copy pathinsert.php
More file actions
63 lines (54 loc) · 1.92 KB
/
Copy pathinsert.php
File metadata and controls
63 lines (54 loc) · 1.92 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
<?php
// // Protection
// $whitelist = array(
// '127.0.0.1',
// '::1'
// );
// // Protection
// if (!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
// header('Location: /');
// }
// Api Fetch
$api_url = 'https://api.coingecko.com/api/v3/coins/bitcoin/';
$json_data = file_get_contents($api_url, true);
$obj = json_decode($json_data);
// DB Connction
$host = "91.218.65.223:3306";
$name = "test";
$user = "konradtest";
$password = "_o0vL93o";
// Connecting to DB
try {
$mysql = new PDO("mysql:host=$host;dbname=$name", $user, $password);
} catch (PDOException $e){
echo "SQL Error: ".$e->getMessage();
}
// Get ranom number between -0.05 and 0.05
$rand = rand(-5, 5) / 100;
// Inserting values into the DB
$stmt = $mysql->prepare("INSERT INTO bitcoin (current_value, current_value_usd) VALUES (:val, :val_usd)");
$stmt->bindParam(":val", $obj->market_data->current_price->eur * (1 + $rand));
$stmt->bindParam(":val_usd", $obj->market_data->current_price->usd * (1 + $rand));
$stmt->execute();
$stmt = $mysql->prepare("SELECT * FROM bitcoin");
$stmt->execute();
$count = $stmt->rowCount();
if ($count > 50) {
$bitcoinData = array();
while ($row = $stmt->fetch()) {
$bitcoinData[$row['id']]['id'] = $row['id'];
$bitcoinData[$row['id']]['time_stamp'] = $row['time_stamp'];
$bitcoinData[$row['id']]['current_value'] = $row['current_value'];
$bitcoinData[$row['id']]['current_value_usd'] = $row['current_value_usd'];
};
$lowest = 9999999;
foreach ($bitcoinData as $btc) {
if ($lowest > $btc['id']) {
$lowest = $btc['id'];
}
}
}
$stmt = $mysql->prepare("DELETE FROM bitcoin WHERE id = :id");
$stmt->bindParam(":id", $lowest);
$stmt->execute();
?>