Q: How to create a table and style it in PHP I have managed to take information from my database of the website, however I have no idea how to present the results in a table and how to style it. I tried putting <table> outside of the whole PHP, clearly did not work. I tried echoing a <table> tag before the result echo and a closing </table> tag after it, but that did not do it. This is the code I am working with:
<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<span style='color:white;'>"."<br> Name: ".$row["name"]."<br> Description: ".$row["description"]."<br> Content: ".$row["content"] ."<br>"."</p>";
        }
    } else {
        echo "0 results";
    }

    $conn->close();
?>

I am still new in PHP, trying to understand how the whole thing works. Thanks in advance!

A: Try this:
<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        echo "<table>";
        echo "<thead>
          <tr>
             <th>Name</th>
             <th>Description</th>
          </tr>
         </thead>";
        echo "<tbody>";
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>".$row["name"]."</td>";
            echo "<td>".$row["description"]."</td>";
            echo "</tr>";            
        }
        echo "</table>";
    } else {
        echo "0 results";
    }

    $conn->close();
?>


A: Try this approach:
[... some HTML header and code ...]

<table>
    <thead>
        <tr><th>Name</th><th>Description</th><th>Content</th></tr>
    </thead>
    <tbody>
    <?php
        [ ... extract something from the database ...]

        while($row = $result->fetch_assoc()) 
        {
            echo "<tr><td>$row[name]</td><td>$row[description]</td><td>Content</td></tr>\n";
        }
    }
    ?>
    </tbody>
</table>

[... some HTML footer ...]

Obviously, replace the [...] sections with your own code.  The idea is here to use PHP to output the dynamic part of your HTML code, and is the power of PHP combined with HTML.
I try to avoid "echoing" to much HTML in my PHP, but it does work (reference other answers).
Detail: when setting up a <table> you should setup a header section <thead> <tfoot> (if applicable) and body section <tbody>.  Ref What is the purpose for HTML's tbody?

A: <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo '<table>';
    echo '<tr><td>Name</td><td>Description</td><td>Content</td></tr>'
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["name"]."</td><td>".$row["description"]."</td><td>".$row["content"] ."</td></tr>";
    }
    echo '</table>';
} else {
    echo "0 results";
}

$conn->close();

?>

A: The basic table format is this
for example:
<table>
    <tr>
        <td>row 1 item 1</td>
        <td>row 1 item 2</td>
    </tr>
    <tr>
        <td>row 2 item 1</td>
        <td>row 2 item 2</td>
    </tr>
</table>

so try:
if ($result->num_rows > 0) {
    echo "<table>";
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>".$row["name"]."</td>";
        echo "<td>".$row["description"]."</td>";
        echo "<td>".$row["content"]."</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}


A: <?php

function tableV1 ($row) {
    echo '<tr>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['content'] . '</td>';
    echo '</tr>';
}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } 
?>

Always do Database Connection first, before Outputting anything, that way, you can create custom error message to show instead of a failed database conntection or no content at all.
<style type="text/css">
table {}
tbody {}
td {}
th {}
thead {}
tr {}
</style>

Style is used inside the <head></head> to style the table, CSS it's called.
<table>
    <thead>
        <th>Name</th>
        <th>Description</th>
        <th>Content</th>
    </thead>
    <tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

    // Output data of each row
    while($row = $result->fetch_assoc()) {
        tableV1($row);
    }

} else {
    echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
    </tbody>
</table>

Output contents from database.
<?php

$conn->close();

?>

Close database connection in the end. All together:
<?php

function tableV1 ($row) {
    echo '<tr>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['description'] . '</td>';
    echo '<td>' . $row['content'] . '</td>';
    echo '</tr>';
}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "onlib";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } 
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style type="text/css">
        table {}
        tbody {}
        td {}
        th {}
        thead {}
        tr {}
        </style>
    </head>
    <body>
        <table>
            <thead>
                <th>Name</th>
                <th>Description</th>
                <th>Content</th>
            </thead>
            <tbody>
<?php
// Takes all the results from the table with genre 5.
$sql = "SELECT name, description, content FROM books WHERE genre='5'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

    // Output data of each row
    while($row = $result->fetch_assoc()) {
        tableV1($row);
    }

} else {
    echo '<tr><td colspan="3">0 results</td></tr>';
}
?>
            </tbody>
        </table>
    </body>
</html>
<?php

$conn->close();

?>

