在html中显示mysql表中的数据

时间:2013-01-03 16:53:10

标签: php html

我需要在html表中显示mysql数据。我目前使用的方法是一个快速修复,有点笨拙,任何人都可以解释这样做的正确方法吗?

<?php

$a= mysql_query ("SELECT * FROM document WHERE email='$semail' ORDER BY  id ASC LIMIT 
");    
$b= mysql_query ("SELECT * FROM document WHERE email='$semail'  ORDER BY id ASC LIMIT
1 OFFSET 1 ");   
$c= mysql_query ("SELECT * FROM document WHERE email='$semail'  ORDER BY id ASC LIMIT
1 OFFSET 2 "); 

if ($a = mysql_fetch_assoc($a));
if ($b = mysql_fetch_assoc($b));
if ($c = mysql_fetch_assoc($c));

?>

<html>
  <table>
     <tr>  
       <td><?php echo $a['col1']; ?></td>
       <td><?php echo $a['col2']; ?></td>
     </tr>   
     <tr>
       <td><?php echo $b['col1']; ?></td>
       <td><?php echo $b['col2']; ?></td>
     </tr>
     <tr>
       <td><?php echo $c['col1']; ?></td>
       <td><?php echo $c['col2']; ?></td>
     </tr>
  </table>
</html>    

1 个答案:

答案 0 :(得分:1)

我相信这正是您在建造桌子时所寻求的:

$command = "SELECT * FROM document WHERE email='$semail' ORDER BY id ASC";
$items = $MySQL->getSQL($command);

echo "<html><body>";

if(count($items) > 0)
{
    echo "<table>";
    foreach($items as $row){
        echo "<tr>";
        foreach($row as $column){
            echo "<td> {$column} </td>";
        }
        echo "</tr>";
    }
    echo "</table>";
}
else echo "NONE";

echo "</body></html>";

我的getSQL函数是:

function getSQL(/*STRING*/ $query)
        {
            $newarray = array();
            $result = mysql_query($query, $this->dbConn);

            if(!$result) die(mysql_error());
            while($row = mysql_fetch_assoc($result))
            { 
                array_push($newarray, $row);
            }

            return $newarray;
        }

这将从您的查询中获取所有内容并为其构建表。你不必把它分成碎片,因为它看起来你已经完成,因为它支持所有的列&amp;行大小。