AlguienEnEsteMundo AlguienEnEsteMundo - 1 year ago 42
HTML Question

Display data from mysql to html page

Im trying to get data from the database into an HTML page using HTML mysql and PHP.

So far, I did a php file with the code and a html where by pressing a button I call that function and show the information in the table (code below).

PHP:

<?php
include 'connection.php';

$sql = "SELECT cName, cEmail, cPhone, cOtherPhone, cAddress, cNeighborhood FROM Client WHERE cEmail='ka@dkdk.com'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["cName"]. "<br>";
echo "Email: " . $row["cEmail"]. "<br>";
echo "Phone number: " . $row["cPhone"]. "<br>";
echo "Other Phone number: " . $row["cOtherPhone"]. "<br>";
echo "Address: " . $row["cAddress"]. "<br>";
echo "Neighborhood: " . $row["cNeighborhood"]. "<br>";
}
} else {
echo "0 results";
}
?>


HTML

<HTML>
<HEAD>
<TITLE>
A Small Hello
</TITLE>
</HEAD>
<BODY>
<div>
<form id="myform" action="/My-code/getMyAccount.php" method="post">

<div class="button">
<button type="submit" name="submit">Update</button>
</div>
</form>
</BODY>
</HTML>


So far all good. Now I want to display the content without pressing any button, so what Im doing is adding that php code in the html:

<HTML>
<HEAD>
<TITLE>
A Small Hello
</TITLE>
</HEAD>
<BODY>
<?php
include 'connection.php';

$sql = "SELECT cName, cEmail, cPhone, cOtherPhone, cAddress, cNeighborhood FROM Client WHERE cEmail='ka@dkdk.com'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["cName"]. "<br>";
echo "Email: " . $row["cEmail"]. "<br>";
echo "Phone number: " . $row["cPhone"]. "<br>";
echo "Other Phone number: " . $row["cOtherPhone"]. "<br>";
echo "Address: " . $row["cAddress"]. "<br>";
echo "Neighborhood: " . $row["cNeighborhood"]. "<br>";
}
} else {
echo "0 results";
}
?>
</BODY>
</HTML>


Im not getting any error, also in the console it writes 200 ok. But no data is display. Any help? Thanks!

Answer Source

inside of your while make a table to format your Output.

change your file extension into .php

    <table>
        <tr>
        <td>Name</td>
        <td>email</td>
        <td>ph_no</td>
        <td>Address</td>
        </tr>
        <?php
        $sql="SELECT * FROM tab_name";
        $result=mysql_query($sql);
        while($row = $result->fetch_assoc()) {
        {
            ?>
            <tr>
            <td><?php echo $row['col1'] ?></td>
            <td><?php echo $row['col2'] ?></td>
            <td><?php echo $row['col3'] ?></td>
            </tr>
            <?php
        }
        ?>
</table>