我有以下代码将数据库列数据显示为表标题
$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/