PHP MySQL表,可重用

PHP MySQL表,可重用,php,mysql,loops,html-table,Php,Mysql,Loops,Html Table,有没有更好的方法来显示mysql表中的数据,而不必先创建表,然后再硬编码标题和表中的字段 我目前正在做的工作的伪代码是 <table> <tr> <th>I am </th><th> harcoded</th> </tr> mysql loop through data for all fields while($row = mysql_fetch_assoc($result)){ <tr>

有没有更好的方法来显示mysql表中的数据,而不必先创建表,然后再硬编码标题和表中的字段

我目前正在做的工作的伪代码是

<table>
<tr>
  <th>I am </th><th> harcoded</th>
</tr>
mysql loop through data
for all fields 
while($row = mysql_fetch_assoc($result)){
  <tr>
   <td>data
   </td>
   <td>data
   </td>
  </tr>
}
</table>

我很抱歉
mysql数据循环
适用于所有领域
while($row=mysql\u fetch\u assoc($result)){
数据
数据
}

您可以使用


编辑:该函数已被弃用,但它确实展示了如何通过发出查询来执行此操作的示例。

我通常会这样做:

$header_done = false;
while($rs = mysql_fetch_assoc($result))
{
    if (!$header_done)
    {
        echo "<tr>";
        foreach ($rs as $k=>$v)
        {
            echo "<td>" . htmlspecialchars ($k) . "</td>";
        }
        $header_done = true;
        echo "</tr>";
    }

    // etc...

}
$header\u done=false;
而($rs=mysql\u fetch\u assoc($result))
{
如果(!$header\u完成)
{
回声“;
foreach($k=>v美元的卢比)
{
echo“.htmlspecialchars($k)”;
}
$header_done=true;
回声“;
}
//等等。。。
}
更新:这是我几年前写的一个函数,我有时会用到它

function EZ_TBL ( $all_rows, $first_row_headers=TRUE )
{

$tr = array ();

if ( $first_row_headers )
{  
    $td = array ();
    foreach ( $all_rows[0] as $k=>$v )
    {  
        if ( $k == 'sort_order' ) continue;
        $td[] = strtoupper ( $k ); 
    }  

    $tr[] = '<td class="header_row">' . implode ( '</td><td class="header_row">', $td ) . '</td>';
}  

usort ( $all_rows, 'sort_by_sort_order' ); 

foreach ( $all_rows as $row )
{  
    $td = array ();
    foreach ( $row as $k=>$v )
    {  
        if ( $k == 'sort_order' ) continue;

        if ( $k == 'url' )
        {  
            $td[] = '<a href="' . $v . '">' . $v . '</a>';  
        } else {
            $td[] = $v; 
        }  
    }  
    $tr[] = '<td>' . implode ( "</td>\n<td>", $td ) . '</td>';
}  

return '<table><tr>' . implode ( "</tr>\n<tr>", $tr ) . '</tr></table>';
}
函数EZ\u TBL($all\u rows,$first\u row\u headers=TRUE)
{
$tr=array();
如果($first_row_headers)
{  
$td=array();
foreach($k=>v的所有_行[0]
{  
如果($k=='sort_order')继续;
$td[]=strtoupper($k);
}  
$tr[]=''。内爆('',$td.'';
}  
usort($all_rows,'sort_by_sort_order');
foreach($所有行作为$行)
{  
$td=array();
foreach($k=>v的行)
{  
如果($k=='sort_order')继续;
如果($k=='url')
{  
$td[]='';
}否则{
$td[]=$v;
}  
}  
$tr[]=''。内爆(“\n”,$td)。'';
}  
返回“”。内爆(“\n”,$tr)。“”;
}

您链接的页面提到mysql\u列表\u字段不推荐使用,而支持使用带有“SQL显示列”的mysql\u查询statement@George皮塔雷利:谢谢你指出这一点,我已经更新了我的答案。干杯