php - 选择并回显所有表格(无论它是什么)

标签 php mysql

我有一个表,我可以选择并回显列的名称 (th) 和字段数据 (td)。但是用户可以添加和删除列。我如何编写可以适应用户更改的更灵活的代码?我的意思是能够在不知道所有字段的情况下拥有整个表。

<?php  
$sql = "SELECT * from eee";
$result = mysql_query($sql,$con);
$id = mysql_field_name($result, 0);
$a = mysql_field_name($result, 1);
$b = mysql_field_name($result, 2);
$c = mysql_field_name($result, 3);
?>

<tr>
<th><input class="gris" type="text" name="<?php echo $id ?>" value="<?php echo $id ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $a ?>" value="<?php echo $a ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $b ?>" value="<?php echo $b ?>"/></th>
<th><input class="gris" type="text" name="<?php echo $c ?>" value="<?php echo $c ?>"/></th>
</tr>

<?php
$result = mysql_query("SELECT * FROM eee");
while($row = mysql_fetch_array($result))  {
?>
<tr>
<td> <input class="blanc" type="text" name="num" value="<?php echo $row['id']?>"/> </td>
<td><input class="blanc" type="text" name="a" value="<?php echo $row['a']?>"/></td>
<td><input class="blanc" type="text" name="b" value="<?php echo $row['b']?>"/></td>
<td><input class="blanc" type="text" name="c" value="<?php echo $row['c']?>"/></td>
</tr>

<?php } ?>

</table>

最佳答案

您正在尝试做的是为穷人提供食物 ORM .我建议您阅读 INFORMATION_SCHEMA .它是一个 ANSI 标准,可以为您提供有关数据库和表的元信息。您可以从那里即时选择列名称,许多现代 RDMS 都支持它。

另一种选择是调查 Doctrine因为它将为您提供此功能。

关于php - 选择并回显所有表格(无论它是什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14058851/

相关文章:

php - 变量后直接回显文本

php - 如何使用 CSS 在图像上创建虚线阴影效果?

mysql - 为什么时间戳字段不能获得默认值?

php - 使用php从保存在mysql中的路径下载视频

php mysql 删除语句

php - 根据 ID 列表与 Eloquent 进行连接

php - 关联数组第二高值与键

c# - 如何在c#中将DataGridView多行数据保存到MySQL数据库

mysql - sql 两个 order by 给出不同的结果

mysql - 将 SQL 创建数据库文件加载到 Docker