php - 将列拆分为单独的变量

标签 php mysql variables

我有以下代码将数据库列数据显示为表标题

 $reportID=$_GET["reportID"];
$result=mysql_query("SELECT * FROM reportFields")or die('Could not connect:loggod line 30 iD' . mysql_error());
 $num_rows = mysql_num_rows($result);   

  echo "    <tbody> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
    $row = mysql_fetch_array($result);

   $field = $row ['field'];

   echo "         
        <th>$field</th> 

        ";  }

    if ($i % 4 == 0) {
        echo ''; // it's time no move to next row
    }

这工作正常,但我想知道如何将每个 $field 作为单独的变量?

有人能帮忙吗?

另外,很抱歉,如果这是一个模糊的问题,但我是新手,可能想做一些令人困惑的事情!

提前致谢

最佳答案

这是一个非常不清楚的问题。而且你有非常奇怪的代码。

只是我对你的要求的猜测:

$result=mysql_query("SELECT * FROM reportFields")or die('Could not connect:loggod line 30 iD' . mysql_error());
echo "<table><thead><tr>";
$i=0; 
$fields = array();
while ($i < mysql_num_fields($result)) {
    $metaField = mysql_fetch_field($result, $i);
    $fields[] = $metaField;
    echo '<th>'.$metaField->name.'</th';
    $i++;
}
echo "</tr></thead>";
echo "<tbody>";
while ($row = mysql_fetch_array($result) )
{
   echo '<tr>';
   foreach( $fields as $field) 
     echo '<td>'. $row [$field->name].'</td>';
   echo '</tr>';
}
echo "</tbody></table>";

关于php - 将列拆分为单独的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30038430/

相关文章:

php打开电子邮件客户端

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

php - 如何使用 PDO 从 href 链接获取单行,收到 fatal error : Call to a member function prepare() on a non-object?

php - 使用函数在 Postgres 中显示来自 Select Count 的数据

php - 如何在 php 中获取 # 之后的 URL anchor 部分

php - 使用 PHP 和 MySQL 的大型动态侧边导航菜单?

mysql - 选择ID后的记录

MySQL 如何在 IN 条件下使用 CONCAT?

node.js - 如何正确地清理 npm 缓存?

php - mysql_query 中变量的通配符运算符