0

I am attempting to print out mySQL database into a html table. I have watched many tutorials on how to do this but am unsure as to how I refer to the html table in my php code. The information gets printed fine and connects to the database but for some reason it isn't output in the table format.

<?php
    $conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');
    if (!$conn) {
        echo "Connection failed:" . mysqli_connect_error();
    }
    //Writing query for database.
    $sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";

    //Querying and getting results
    $result = mysqli_query($conn, $sql);

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            echo "<tr><td>" . $row["First Name"] . "</td></tr>" . $row["Last Name"] . "</td></tr>"
                . $row["Emails"] . "</td></tr>" . $row["Date Created"] . "</td></tr>";
        }
        echo "</table>";
    } else {
        echo "0 result";
    }

    //Fetch resulting rows as an array
    $informed = mysqli_fetch_all($result, MYSQLI_ASSOC);

    // Freeing result from the memory.
    mysqli_free_result($result);

    mysqli_close($conn);
?>

<!DOCTYPE html>
<html lang="en-US">
    <head>
        <div class="Contained">
            <div class="row">
                <?php foreach ($informed as $inform) { ?>
                    <div class="col s6 medium-3">
                        <div class="card z-depth-0">
                            <div class="card-content center">
                                <h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>
                                <div><?php echo htmlspecialchars($inform['Last Name']); ?></div>
                            </div>
                            <div class="card-action right-align">
                                <a class="brand-text" href="#">More Info
                            </div>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
        <title> Email and Name List </title>
    </head>
    <body>
        <table>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Emails</th>
                <th>Date Created</th>
            </tr>
        </table>
    </body>
</html>

Output in browser gyazo:

1
  • loop your data in between <table> Commented Jan 17, 2020 at 12:19

2 Answers 2

1

You must change code for output all table in php like:

<body>
<?php


$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');

if (!$conn){
echo "Connection failed:" . mysqli_connect_error();
}
//Writing query for database.
$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date 
Created`";

//Querying and getting results

$result = mysqli_query($conn,$sql);

if ($result->num_rows>0){
echo '
<table>
<tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Emails</th>
    <th>Date Created</th>
</tr>';
    while($row = $result->fetch_assoc()){
        echo "<tr> ";
        echo "<td>" . $row["First Name"] . "</td>";
        echo "<td>" . $row["Last Name"] . "</td>";
        echo "<td>" . $row["Date Created"] . "</td>";
        echo "</tr> ";
        }

    echo"</table>";
}
else{
    echo "0 result";
}

//Fetch resulting rows as an array

$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Freeing result from the memory.

mysqli_free_result($result);

mysqli_close($conn);

?>

</body>

Another question are you sure is $row["First Name"] and not $row["First_Name"]?
Last tip learn how prepare stm for prevent sql inject

Sign up to request clarification or add additional context in comments.

5 Comments

This outputs the data but again it does not do it in the table format. All the data is output under the column First_Name see: gyazo.com/9e409ad09464dde49b9154cf1997a200 could this be a problem with my sql database? I thought I had set it up correctly
@SimoneRossaini Can you point out exactly how the code is vulnerable to SQL injection when there are no parameters in the query?
You forgot the 'Emails' row in your echo loop but yep all works perfect! Thanks man this is gonna help me so much.
@symcbean see here all reference
@SimoneRossaini No. I asked where, in the code presented by the OP, is there a SQLI vulnerability - which was intended as a polite way of pointing out that you are providing misleading and irrelevant information in your answer.
0

Based on your code, you are trying to print the table before the actual table is defined below. You can try something like this:

<?php

    $conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');

    if (!$conn){
        echo "Connection failed:" . mysqli_connect_error();
    }

    //Writing query for database.
    $sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";

    //Querying and getting results

    $result = mysqli_query($conn,$sql);

    $informed = mysqli_fetch_all($result, MYSQLI_ASSOC); 
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<div class="Contained">
<div class="row">

    <?php foreach($informed as $inform){?>
        <div class="col s6 medium-3">

            <div class="card z-depth-0">
                <div class="card-content center">
                    <h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>
                    <div><?php echo htmlspecialchars($inform['Last Name']);?></div>
                </div>
                <div class="card-action right-align">
                <a class="brand-text" href="#">More Info</div>

            </div>

        </div>
    <?php }?>

</div>
</div>

<title> Email and Name List </title>
</head>
<body>
    <table>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Emails</th>
            <th>Date Created</th>
        </tr>
        <?php 
              if ($result->num_rows>0) {
                  while($row = $result->fetch_assoc()){
                      echo "<tr><td>" . $row["First Name"] . "</td><td>" .  $row["Last Name"] . "</td><td>" . $row["Emails"] . "</td><td>" . $row["Date Created"] . "</td></tr>";
                  }
              } else {
                  echo "<tr><td rowspan=\"5\">0 result</td></tr>";
              }
        ?>
    </table>
</body>

<?php 
    // Freeing result from the memory.
    mysqli_free_result($result);
    mysqli_close($conn);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.