dpmfur2635 2013-05-13 09:33
浏览 34
已采纳

mysqli_query打印无理由? [关闭]

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.

  • 写回答

3条回答 默认 最新

  • dqjcb132285 2013-05-13 09:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?