简体   繁体   中英

Display data from mysql to html page

I am trying to get data from the database into an HTML page using HTML MySQL and PHP.

I created 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 is ok. Now I want to display the content without pressing any button. What I am 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>

I am not getting any error, also in the console it writes 200 ok. But no data is display. How can I resolve this?

Html page not support php tag so You have to create a php file then try with below code :

<?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";
}
?>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM