-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions-shortcode-addition.php
More file actions
46 lines (37 loc) · 1.53 KB
/
functions-shortcode-addition.php
File metadata and controls
46 lines (37 loc) · 1.53 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
function shortcode_sqltable() {
ob_start();
// Database connection settings
$host = "localhost"; // Change if needed
$dbname = "databasename"; // Change to your DB name
$username = "your_username";
$password = "your_password";
try {
// Create la connection
$pdo = new PDO(
"mysql:host=$host;dbname=$dbname;charset=utf8mb4",
$username,
$password,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]
);
// SQL query to DB
$sql = "SELECT id, name, betrag, grund FROM bestand";
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<tr><th>Lfd. Nr</th><th>Name</th><th>Betrag</th><th>Grund</th></tr>"; // Change the name of the columns, remove or add more columns
foreach ($pdo->query($sql) as $row) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['id']) . "</td>";
echo "<td>" . htmlspecialchars($row['name']) . "</td>";
echo "<td>" . htmlspecialchars($row['betrag']) . "</td>";
echo "<td>" . htmlspecialchars($row['grund']) . "</td>";
echo "</tr>";
}
echo "</table>";
} catch (PDOException $e) {
echo "<p style='color:red;'>Error whilst connecting to Database: " . htmlspecialchars($e->getMessage()) . "</p>";
}
return ob_get_clean(); // Return output
}
add_shortcode('sqltable', 'shortcode_sqltable');