I am new to PHP programming and i try to learn how to work with databases. I have MySQL ready, with 1 database, containing 1 table which contains 7 entries. The problem is that when i try to output the entries to a very simple table, before the creation of the table it output 7 times < BR /> without reason. The code is like this:
<?php
$con=mysqli_connect("localhost","root","**********", "*****");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else { echo "Connection was OK! <br/>";}
$result = mysqli_query($con,"SELECT * FROM pelates") or die(mysql_error());
echo "<table border='1'><tr><th>ID</th><th>Onoma</th><th>Epwnymo</th><th>Hlikia</th><th>Genethlia</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr> <td>" . $row['id'] . "</td> <td>" . $row['name'] . "</td> <td>" . $row['surname'] . "</td> <td>" . $row['age'] . "</td> <td>" . $row['birthday'] . "</td></tr>";
echo "<br>";
}
echo "</table>";
mysqli_close($con);
?>
Everything i believe looks good. Here is part of the output:
Connection was OK! <br/><table border='1'><tr><th>ID</th><th>Onoma</th>
As you can see the is only 1 < br /> between the message "Connection was OK" and the table. In real it produce another 7! Here is the result of inspect element in Chrome (same as Firefox too!):
Connection was OK!
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<table border='1'><tr><th>
As i understand, when i define:
$result = mysqli_query($con,"SELECT * FROM pelates") or die(mysql_error());
It takes 7 results into "memory", but this is supposed to not printed in real. Why it prints 7 breakrows, while i didn't told it to? And what can i do to prevent it?
Thank you very much for your time, and sorry for the long post.
You put <br>
between rows in table </tr><br/><tr>.
As you mentioned inspect element both in FF and Chrome shows them before table. It's because <br>
doesn't belong there and browser has to put it somewhere so it puts them before table.
If you would display source (instead of inspecting elements), you'd get correct result.
Remove echo "<br>";
inside while loop
Its all because of this line
echo "<br>";
Remove it.
The reason is because you haven't putted <br/>
inside <td></td>
tags. So it comes before <table>
.