Reputation: 123
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?
Upvotes: 1
Views: 273
Reputation: 528
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>
Upvotes: 2
Reputation: 139
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";
}
?>
Upvotes: 1