← tagasi näidete juurde

PHP demo — DBE backendina

See leht on päris PHP-fail serveris (/databases/php/index.php), mis iga laadimisel teeb HTTP/JSON päringu samas masinas jooksvale DBE Flask-rakendusele, lisab ühe rea andmebaasi php tabelisse kasutajad ja näitab selle hetkeseisu.

Tabel "kasutajad" (viimased 10 rida)

Andmeid ei õnnestunud laadida — kontrolli, kas DBE Flask rakendus (port 8001) jookseb.

Lähtekood

<?php
$base = "http://127.0.0.1:8001";

function dbeRequest($path, $body) {
    global $base;
    $ch = curl_init($base . $path);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}

dbeRequest("/api/db/php/execute", [
    "script" => "LISA TABELISSE kasutajad (id, nimi) VÄÄRTUSED (" . time() . ", 'Külaline');",
]);

$select = dbeRequest("/api/db/php/execute", [
    "script" => "VALI KÕIK TABELIST kasutajad ORDER BY id DESC LIMIT 10;",
]);

$columns = [];
$rows = [];
if ($select && isset($select["results"][0]) && $select["results"][0]["ok"]) {
    $columns = $select["results"][0]["columns"];
    $rows = $select["results"][0]["rows"];
}
?>
<!doctype html>
<html lang="et">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PHP demo &mdash; DBE</title>
<style>
  * { box-sizing: border-box; }
  body { font-family: system-ui, -apple-system, "Segoe UI", sans-serif; background:#0f1115; color:#e6e6e6; margin:0; }
  main { max-width: 800px; margin: 0 auto; padding: 24px; }
  h1, h2 { color:#f0f0f0; }
  .panel { background:#161a22; border:1px solid #2a2f3a; border-radius:8px; padding:18px 20px; margin-bottom:20px; }
  table { border-collapse: collapse; width: 100%; margin: 8px 0; }
  th, td { border: 1px solid #2a2f3a; padding: 6px 10px; text-align: left; font-size: 0.9rem; }
  th { background:#1d2230; }
  pre { background:#0f1115; border:1px solid #2a2f3a; border-radius:6px; padding:12px 14px; overflow-x:auto; font-size:0.8rem; white-space: pre-wrap; }
  a { color:#7cc4ff; }
  .muted { color:#888; }
</style>
</head>
<body>
<main>
  <p><a href="/naited.html">&larr; tagasi n&auml;idete juurde</a></p>
  <h1>PHP demo &mdash; DBE backendina</h1>
  <p class="muted">
    See leht on p&auml;ris PHP-fail serveris (<code>/databases/php/index.php</code>),
    mis iga laadimisel teeb HTTP/JSON p&auml;ringu samas masinas jooksvale DBE
    Flask-rakendusele, lisab &uuml;he rea andmebaasi <code>php</code> tabelisse
    <code>kasutajad</code> ja n&auml;itab selle hetkeseisu.
  </p>

  <section class="panel">
    <h2>Tabel &quot;kasutajad&quot; (viimased 10 rida)</h2>
    <?php if ($rows): ?>
      <table>
        <thead>
          <tr><?php foreach ($columns as $c): ?><th><?= htmlspecialchars($c, ENT_QUOTES, 'UTF-8') ?></th><?php endforeach; ?></tr>
        </thead>
        <tbody>
          <?php foreach ($rows as $row): ?>
            <tr><?php foreach ($columns as $c): ?><td><?= htmlspecialchars((string)$row[$c], ENT_QUOTES, 'UTF-8') ?></td><?php endforeach; ?></tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php else: ?>
      <p>Andmeid ei &otilde;nnestunud laadida &mdash; kontrolli, kas DBE Flask rakendus (port 8001) jookseb.</p>
    <?php endif; ?>
  </section>

  <section class="panel">
    <h2>L&auml;htekood</h2>
    <pre><?= htmlspecialchars(file_get_contents(__FILE__), ENT_QUOTES, 'UTF-8') ?></pre>
  </section>
</main>
</body>
</html>