-1

我需要在 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>    
4

1 回答 1

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;
        }

这将从您的查询中获取所有内容并为其构建一个表。您不必将它分解成碎片,因为它支持所有列和行大小。

于 2013-01-03T17:16:56.893 回答