<?php
$db = mysqli_connect('localhost', 'root', '', 'biblioteka');
if (!$db) {
    die("Connection failed: " . mysqli_connect_error());
}
?>
    <!doctype html>
    <html lang="pl">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Kwerenda 4</title>
        <style>td, th, table { border: 1px black solid; border-collapse: collapse}</style>
    </head>
    <body>
    <h2>Kwerenda 4</h2>
    <p>Kwerenda: <code>SELECT autor, tytul, kod FROM ksiazki ORDER BY RAND() LIMIT 5;</code></p>
    <?php
    $query = "SELECT autor, tytul, kod FROM ksiazki ORDER BY RAND() LIMIT 5;";

    $result = mysqli_query($db, $query);
    if (mysqli_num_rows($result) != 0) {
        echo "<table>";
        echo "<tr><th>Autor</th><th>Tytul</th><th>Kod</th></tr>";
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr>";
            echo "<td>" . $row['autor'] . "</td><td>" . $row['tytul'] . "</td><td>" . $row['kod'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "Brak wyników.";
    }
    ?>
    </body>
    </html>

<?php
mysqli_close($db);