0

因此,此 PHP 代码将在网页上打印我的数据库中的所有表数据。

<?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>";

}
?>

但本质上它所做的就是这样。它的格式不正确。它是这样的。

无格式表

我想做的就是把这张桌子准确地放在网页的中心,给它一些花哨的边框(弯曲的边缘,很棒的字体。)

4

6 回答 6

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>";
于 2013-06-07T09:41:36.490 回答
1

您正在为通过行的每次迭代创建一个新表。将<table>标签移出循环。

于 2013-06-07T09:40:01.010 回答
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>";
于 2013-06-07T09:41:22.633 回答
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>

为了使表格正确居中,有必要为其设置宽度。否则它将无法正常工作。

于 2013-06-07T10:04:43.987 回答
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>';
于 2013-06-07T10:08:35.017 回答
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>
于 2013-06-07T09:46:45.410 回答