1

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='[email protected]'";
$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='[email protected]'";
$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?

9
  • 1
    you cannot use php code in html Commented Aug 24, 2016 at 12:21
  • Did you use the file extension php? php code won't execute if the file is an html file. Commented Aug 24, 2016 at 12:22
  • where you are running your this code? Commented Aug 24, 2016 at 12:23
  • what's output? can u please share Commented Aug 24, 2016 at 12:24
  • Ahh... so if I have the php file created, how can I use that file? I mean I did it but when the user press a button, I dont want the user to press anything, I want the page to load directly the output Commented Aug 24, 2016 at 12:25

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

the whole part about the table has nothing to do with his problem.
don't put it in your answer, if you have other comments about his code leave them as a comment. Don't present it as part of the solution to the actual problem? makes it harder for new people to actually learn which part of your answer fixed their problem
1

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='[email protected]'";
$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";
}
?>

2 Comments

That code works in a .php file, but now I want that when I open the page see the information, with no need to press a button, understand?
Not able to understand your question Let me know example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.