我想根据循环中的字段名称做一些不同的事情。我的代码根据 sql 查询中使用的字段动态生成一个 html 表。如果循环中的字段是主键,我想生成某个页面的链接...有什么想法吗?
我已经用 HERE 标记了需要获取字段名称的位置。
if (mysql_num_rows($result)>0) {
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($result)) {
$out .= "<th bgcolor='#CFCFCF'><font size=2>". mysql_field_name($result, $i) . "</font></th>";
$i++;
}
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC)) {
$out .= "<tr>";
foreach ($rows as $data) {
//HERE
$out .= "<td bgcolor='#DCDCDC'><font size=2>". $data . "</font></td>";
}
}
}
最佳答案
我认为做到这一点的唯一方法是执行 SHOW COLUMNS FROM tablename;
事先询问。
结果中的“关键”列将为您提供每列的关键状态。您可以从那里找到并保存主键的位置,并相应地输出该列的 HTML。
关于php、mysql,如何获取动态 sql html 表循环内的字段名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3902010/