因此,此PHP代码将在Web页面上打印我数据库中的所有表数据。
<?php
$hostname = '127.0.0.1:3306';
$dbname = 'login'; // Your database name.
$username = 'root'; // Your database username.
$password = ''; // Your database password. If your database has no password, leave it empty.
mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
mysql_select_db($dbname) or DIE('Database name is not available!');
$query="SELECT * FROM markers";
$result=mysql_query($query);
$fields_num = mysql_num_fields($result);
echo "<table border='2'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td></div>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<table>";
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "</tr>\n";
echo "</table></div>";
}
?>
但基本上它正在做的是。它格式不正确。就是这样。
我想要做的是将此表准确地放在网页的中心,给它一些花哨的边框(弯曲的边缘,很棒的字体。)
答案 0 :(得分:2)
您无需为要输出的每一行重新创建表格,因此请更改
while($row = mysql_fetch_row($result))
{
echo "<table>";
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "</tr>\n";
echo "</table></div>";
}
到
echo "<div><table>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "</tr>\n";
}
echo "</table></div>";
答案 1 :(得分:1)
您正在为每次迭代创建一个新表。将<table>
标记移到循环外部。
答案 2 :(得分:0)
更改
while($row = mysql_fetch_row($result))
{
echo "<table>";
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "</tr>\n";
echo "</table></div>";
}
到
echo "<table>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "</tr>\n";
}
echo "</table>";
答案 3 :(得分:0)
您不需要在循环中放置表头,因为标头始终是相同的。对于标题,您可以尝试:
<table border="1" align="left">
<tr>
<th>Employee ID</th>
<th>Designation ID</th>
<th>First Name</th>
</tr>
if(!result)
while($row = mysql_fetch_row($result))
{
<tr>
<td><?php $row[0];?></td>
</tr>
}
</table>
答案 4 :(得分:0)
使用
echo "<table>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
echo "<td>$row[3]</td>";
echo "</tr>\n";
}
echo "</table>";
然后在文档的头部,使用以下代码:
<style>
table {
margin: 0 auto;
width: 50%;
}
</style>
为了使表格正确居中,需要为其设置宽度。否则它将不起作用。
答案 5 :(得分:0)
你需要2个循环(第一个内部的第二个),
$result = mysqli_query('your sql');
// begin table, only once
echo '<table>';
// use thead for the table header :)
echo '<thead>';
for($i=0; $i<$fields_num; $i++)
{
$field = mysqli_fetch_field($result);
echo '<th>'.$field->name.'</th>';
}
echo '</thead>';
echo '<tbody>';
// 1st loop, each rows
while($row = mysqli_fetch_assoc($result))
{
// each rows start with <tr> / ends with </tr>
echo '<tr>';
// 2nd loop, each field
foreach($row as $field => $value)
{
// you can also add class name in each case of your table, to add custom style
// for each field in your css
echo '<td class="'.$field.'">'.$value.'</td>';
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';