在表中打印查询结果

时间:2010-01-04 14:24:08

标签: php mysql

如果我有一个名为“info”的MySQL表,如下所述,我想打印出如下所述的HTML表格,我该怎么做?

MySQL表中的字段:

id subject category actions date status

HTML表格结构:两列,首先包含字段“subject”,其次包含字段“actions”,按“actions”降序排序。仅显示“类别”与用户输入的变量“$ find”

相匹配的条目

这是我要开始的地方,但我不知道下一步该去哪里:

$result=mysql_query("SELECT subject, actions FROM info WHERE category='$find' ORDER BY votes DESC")
or die(mysql_error());

if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){

提前致谢,

约翰

1 个答案:

答案 0 :(得分:0)

像这样:

<?php
$result = mysql_query("SELECT subject, actions FROM info WHERE category='$find' ORDER BY votes DESC") or die(mysql_error());

if(mysql_num_rows($result) > 0): ?>
<table>
    <tr>
        <th>Subject</th>
        <th>Actions</th>
    <tr>
    <?php while($row = mysql_fetch_assoc($result)): ?>
    <tr>
        <td><?php echo $row['subject']; ?></td>
        <td><?php echo $row['actions']; ?></td>
    </tr>
    <?php endwhile; ?>
</table>
<?php endif; ?>