Webshop/src/backend/example/database_calls/getDatabaseTable.php
2024-10-10 22:03:35 +02:00

39 lines
975 B
PHP

<?php
$hostname = "hostname/IP";
$username = "username";
$password = "password";
$db_name = "databaseName";
$port = "port";
$sqlArray = [];
// get PHP config
$root_pfad = $_SERVER["DOCUMENT_ROOT"] . "/core";
require_once $root_pfad . "/config/global.inc.php";
header("Content-Type: text/html; charset=utf-8");
// start new connection to database
$conn = new mysqli($hostname, $username, $password, $db_name, $port);
$conn->set_charset("utf8mb4");
// close connection on error
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare SQL statement and execute on database
$sql = "SELECT * FROM table";
// get data from database
$result = $conn->query($sql);
$conn->close();
// check if result is empty & has more than "0" rows
if (!empty($result) && (int)$result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$sqlArray[] = $row;
}
echo json_encode($sqlArray);
} else {
echo "0 results";
}
exit();